9

Are there any tool that will compile a java .properties file to a class which I can use in Java EE (tomcat) application? Similar to android where the eclipse plugin produces a static R.strings class.

I found this article:

http://www.techhui.com/profiles/blogs/localization-in-gwt-using

But it is dependant on GWT. Any help appreciated.

  • Can you explain a bit. Do you mean a class containing constants (like enum) or just a property holder? – questzen Mar 29 '12 at 12:10
  • This post seems quite similar to what you are asking: http://stackoverflow.com/questions/1578026/java-properties-files-as-strongly-typed-classes – paul-g Oct 18 '14 at 17:31

6 Answers6

3

I have never heard about such tool. GWT has a great deferred-binding based technique but it is not the thing you are looking for. However I think it is possible to implement a basic code generator for such tasks.

But the answer to your question is: as far as I know there isn't.

jabal
  • 11,987
  • 12
  • 51
  • 99
  • +1 for code generation using a small java application, that you can automatically invoke from an ant task which you can add as an automatically invoked builder in eclipse. – jabal Sep 01 '14 at 17:50
1

To internationalize applications I implemented a Message Compiler, which creates the resource bundle files and constant definitions as Java enums or static final strings for the keys from one single source file. So the constants can be used in the Java source code, which is a much safer way than to use plain strings. In this case you also get a compile time error, when you use a key constant, that doesn't exist. The message compiler cannot only be used for Java. It creates also resource files and constants for Objective-C or Swift and can be extended for other programming environments.

thmayr
  • 57
  • 6
0

What about ResourceBundle?

// refers to "src/config.properties"
ResourceBundle config = ResourceBundle.getBundle("config"); 
String property1 = config.getString("property1");
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • 1
    As with the other recent answer to this question, this does not answer this question. What you described is how it's usually done. But in android you get a generated class with all the resources in compile time (R.id). If property1 is not defined you would get a runtimeexception here. If it's not defined in Android you would get a compile time error. – jontro Oct 17 '14 at 11:59
0

I think one could write a very simple grammar for properties files using ANTLR in a custom a maven plugin (or Ant task) that just generates the Java source before the compilation step.

Jim
  • 668
  • 8
  • 22
0

How about storing your properties in a JSON file. The JSON object stored in the file should map to a Java class, use Jackson mapper to deserialize. With Jackson you can enforce that all fields must be non-null on deserialize. You can also use GSON and write a custom deserializer that performs checks as strict as you want them. Example - you can enforce not null along with not empty for strings.

tinker
  • 1,396
  • 11
  • 20
0

Compiler Assisted Localization (CAL10N) is not exactly what you asked, but may be of help.

Although it does not generate Java classes from .properties, using enums as message keys is still better than strings, as you get some help from the compiler.

Declare a enum, bind it to .properties with annotation and use enum values in message lookups. I have not tried it yet, though. See manual.

Alex
  • 367
  • 1
  • 3
  • 17