java - Configuring string externalization in Eclipse to use ${key} as field name -
suppose have simple code this:
public class externalizestringdemo { public static void main(string[] args) { system.out.println("hello world"); } }
now, want externalize greeting, perhaps facilitate internationalization/localization/etc. using eclipse, can use string externalization wizard (source/externalize strings), , configure this:
i can proceed wizard , 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 simple: can ask eclipse externalize access use key field names instead? is, want access e.g. messages.demo_greeting
.
note: if [substitution pattern]
simple ${key}
, generated code messages."demo_greeting"
, not valid java code.
if not possible, what's next best thing? (i'm thinking eclipse regex find/replace?).
eclipse has new string externalization mechanism this; uses own new message bundle instead of java's. need include org.eclipse.osgi….jar
in project's build path use it.
help.eclipse.org
- java development user guide > reference > wizards , dialogs > externalize strings wizard
- use eclipse's string externalization mechanism
- if unchecked standard externalization mechanism used, otherwise new eclipse string externalization mechanism used.
- note: present if project build path contains
org.eclipse.osgi.util.nls
the before-and-after shown in feature documentation:
old code:
public class myclass { public void mymethod() { string message; ... // no args message = messages.getstring("key.one"); //$non-nls-1$ ... // bind 1 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 1 arg message = nls.bind(messages.key_two, "example usage"); //$non-nls-1$ ... } }
screenshots
the setup:
then proposed changes:
Comments
Post a Comment