Generate string constants from property file keys

I use .properties files to internationalize messages. For instance:

HELLO_WORLD = Hello World HELLO_UNIVERSE = Hello Universe 

And then in the Java code:

 String foo = resourceBundle.getString("HELLO_WORLD"); 

String literals like "HELLO_WORLD" are problematic because they are error prone and cannot be autocompleted. I would like to generate the code from the keys in the properties file as follows:

 public interface Messages { // Or abstract class with private constructor public static final String HELLO_WORLD = "HELLO_WORLD"; public static final String HELLO_UNIVERSE = "HELLO_UNIVERSE"; } 

And then use it like this:

 String foo = resourceBundle.getString(Messages.HELLO_WORLD); 

Is there a standard way to do this? I prefer the Maven plugin, but any standalone tool that I can run manually will be good enough for my needs.

+5
source share
3 answers

The following code will generate the MyProperties interface in the root directory of the project, after which you can use this interface anywhere.

 public class PropertiesToInterfaceGenerator { public static void main(String[] args) throws IOException { Properties properties = new Properties(); InputStream inputStream =PropertiesToInterfaceGenerator.class.getClassLoader().getResourceAsStream("xyz.properties"); if(null != inputStream ){ properties.load(inputStream); } generate(properties); } public static void generate(Properties properties) { Enumeration e = properties.propertyNames(); try { FileWriter aWriter = new FileWriter("MyProperties.java", true); aWriter.write("public interface MyProperties{\n"); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String val = properties.getProperty(key); aWriter.write("\tpublic static String "+key+" = \""+val+"\";\n"); } aWriter.write(" }\n"); aWriter.flush(); aWriter.close(); }catch(Exception ex){ ex.printStackTrace(); } } } 
+2
source

Best vice versa:

 public enum Message { HELLO_WORLD, HELLO_UNIVERSE; public String xlat(Locale locale) { resourceBundle.getString(toString(), locale); } } 

Create a property template from this enumeration. This can be repeated for new posts if your base language is in a separate ..._en.properties .

Generation can be performed using values ​​() - indiscriminately. Although, perhaps you want to introduce some annotation of comments on properties or the like.

+3
source

No, no one has ever written this kind of plugin that has all the features that you posted, because:

  • Internationalization can have many entries, and in the end you get a giant class, interface, enumeration, or something else that is bad.
  • The maven / gradle plugin creates classes for you, but only at compile time. I see that you mentioned autocomplete, which means you will also need an IDE plugin, which means that the plugin for the building tool (gradle / ant / ...) is not enough. The interaction between these plugins may be error prone.
  • Later in the project, if you or your colleagues want a new translation entry, you will have to update the classes. This is a little tiring.

When working with internationalization, something like i18n is recommended. If you do not want the new library or your project to be small, you can use the eclipse externalization function to do this, see

Andriod: External Strings for Android Project

Other: help.eclipse.org - Java Development Guide> Help> Wizards and Dialogs> External String Wizard

+1
source

All Articles