Configuring string externalization in Eclipse to use ${key} as field name

别来无恙 提交于 2019-12-04 17:08:31

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!