在Eclipse中配置字符串外部化以使用$ {key}作为字段名称 [英] Configuring string externalization in Eclipse to use ${key} as field name
问题描述
public class ExternalizeStringDemo {
public static void main [] args){
System.out.println(Hello world);
}
}
现在,我想外发化问候语,也许促进国际化/本地化等。使用Eclipse,我可以使用字符串外部化向导(源/外部化字符串),并配置如下:
我可以继续向导,并提出这些更改:
- 创建文件
个人玩具/ src / Messages.java
- 创建文件
个人玩具/ src / messages.properties
- 编辑
ExternalizeStringDemo.java
-
Hello World
成为Messages.getString(DEMO_GREETING )
-
我的问题很简单:要求Eclipse将访问权利外部化以使用密钥作为字段名称?也就是说,我希望访问是例如 Messages.DEMO_GREETING
。
注意:如果 [替换模式]
是简单的 $ {key}
,那么生成的代码是消息。DEMO_GREETING
一个有效的Java代码。
如果这不可能,那么下一件最好的事情是什么? (我正在考虑Eclipse正则表达式查找/替换?)
Eclipse有一个新的字符串外部化机制,它使用自己的新消息包而不是Java。您需要在项目的构建路径中包含 org.eclipse.osgi ... .jar
。
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 isMessages."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?).
解决方案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.
help.eclipse.org
- Java development user guide > Reference > Wizards and Dialogs > Externalize Strings Wizard
- Use Eclipse's string externalization mechanism
- If unchecked the standard externalization mechanism is used, otherwise the new Eclipse string externalization mechanism is used.
- Note: Only present if the project build path contains
org.eclipse.osgi.util.NLS
The before-and-after is shown in the feature documentation:
Old Code:
public class MyClass { public void myMethod() { String message; ... // no args message = Messages.getString("key.one"); //$NON-NLS-1$ ... // bind one arg message = MessageFormat.format( Messages.getString("key.two"), new Object[] {"example usage"} ); //$NON-NLS-1$ //$NON-NLS-2$ ... } }
New Code:
public class MyClass { public void myMethod() { String message; ... // no args message = Messages.key_one; ... // bind one arg message = NLS.bind(Messages.key_two, "example usage"); //$NON-NLS-1$ ... } }
Screenshots
The setup:
Then the proposed changes:
Related links
这篇关于在Eclipse中配置字符串外部化以使用$ {key}作为字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!