user75832
user75832

Reputation:

Java: Static string localization

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.

Upvotes: 9

Views: 1122

Answers (6)

thmayr
thmayr

Reputation: 57

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.

Upvotes: 1

Alex
Alex

Reputation: 377

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.

Upvotes: 0

tinker
tinker

Reputation: 1406

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.

Upvotes: 0

Jim
Jim

Reputation: 677

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.

Upvotes: 0

Stefan
Stefan

Reputation: 12453

What about ResourceBundle?

// refers to "src/config.properties"
ResourceBundle config = ResourceBundle.getBundle("config"); 
String property1 = config.getString("property1");

Upvotes: 0

jabal
jabal

Reputation: 12347

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.

Upvotes: 3

Related Questions