1

Programming in Java, what's the better way to create resource files to save the messages of the application in different languages?

If I use a properties file, inside the code everytime that I need refer to a message I have to write something like that:

ResourceBundle.getBundle("test.messages.messageFile").getString("Message1")

Exists another form to use messages and generate a code cleaner? Maybe create a class with static constants that refer to this messages?

3 Answers3

0

you can use ResourceBundle, but declare it and use it as a variable.

ResourceBundle languageBundle=ResourceBundle.getbundle("test.messages.messageFile");

//later in your code you can say

languageBundle.getString("Message1");

it would help if you make this variable public static

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
0

This is the standard way. But nothing forces you to not store the bundle in a field:

private ResourceBundle messages = ResourceBundle.getBundle("test.messages.messageFile");

...

String message1 = messages.getString("Message1");

You may also encapsulate it into a Messages class that has the following method:

public String getMessage1() {
    return messages.getString("Message1");
}

It's all up to you.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

you can try Internationalization made easier.

This is based on annotations and gives type safety for argument substitution.

Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22