在 Eclipse 中配置字符串外部化以使用 ${key} 作为字段名称
假设我有一个像这样的简单代码:
public class ExternalizeStringDemo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
现在,我想将问候语外部化,也许是为了促进国际化/本地化等。使用 Eclipse,我可以使用字符串外部化向导(Source/Externalize Strings),并按如下方式配置它:
I可以继续执行向导,它将建议进行以下更改:
- 创建文件
Personal Toys/src/Messages.java
- 创建文件
Personal Toys/src/messages.properties
- 编辑
ExternalizeStringDemo.java
“Hello World”
变为Messages.getString("DEMO_GREETING")
我的问题很简单:我可以要求 Eclipse 外部化访问以使用密钥作为字段名字代替?也就是说,我希望访问权限为 Messages.DEMO_GREETING
。
注意:如果[Substitution pattern]
是简单的${key}
,那么生成的代码是Messages."DEMO_GREETING"
,这不是有效的 Java 代码。
如果这不可能,那么下一个最好的办法是什么? (我在想 Eclipse 正则表达式查找/替换?)。
Suppose I have a simple code like this:
public class ExternalizeStringDemo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Now, I want to externalize the greeting, perhaps to facilitate internationalization/localization/etc. Using Eclipse, I can use the String Externalization wizard (Source/Externalize Strings), and configure it like this:
I can proceed with the wizard and it will propose these changes:
- Create file
Personal Toys/src/Messages.java
- Create file
Personal Toys/src/messages.properties
- Edit
ExternalizeStringDemo.java
"Hello World"
becomesMessages.getString("DEMO_GREETING")
My question is simple: can I ask Eclipse to externalize the access to use the key as field names instead? That is, I want the access to be e.g. Messages.DEMO_GREETING
.
Note: if the [Substitution pattern]
is simple ${key}
, then the generated code is Messages."DEMO_GREETING"
, which is not a valid Java code.
If this is not possible, then what's the next best thing? (I'm thinking Eclipse regex find/replace?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Eclipse 有一个新的字符串外部化机制来完成这个任务;它使用自己的新消息包而不是 Java 的消息包。您需要将 org.eclipse.osgi….jar 包含在项目的构建路径中才能使用它。
之前和之后显示在 功能文档:
屏幕截图
设置:
然后建议的更改:
相关链接
Eclipse has a new string externalization mechanism that does exactly this; it uses its own new message bundle instead of Java's. You need to include the
org.eclipse.osgi….jar
in your project's build path to use it.The before-and-after is shown in the feature documentation:
Screenshots
The setup:
Then the proposed changes:
Related links