Java
To reteive localized text (messages), use the java.util.ResourceBundle
API. To format messages, use java.text.MessageFormat
API.
Basically, first create a properties file like so:
key1 = Your input {0} is excellent!
key2 = You were {0} old when you switched from {1} to {2} shows.
The {n}
things are placeholders for arguments as you can pass in by MessageFormat#format()
.
Then load it like so:
ResourceBundle bundle = ResourceBundle.getBundle("filename", Locale.ENGLISH);
Then to get the messages by key, do:
String key1 = bundle.getString("key1");
String key2 = bundle.getString("key2");
Then to format it, do:
String formattedKey1 = MessageFormat.format(key1, "xyz");
String formattedKey2 = MessageFormat.format(key2, "4 years", "Barney and Friends", "Spongebob");
See also:
Android
With regards to Android, the process is easier. You just have to put all those String
messages in the res/values/strings.xml
file. Then, you can create a version of that file in different languages, and place the file in a values
folder that contains the language code. For instance, if you want to add Spanish support, you just have to create a folder called res/values-es/
and put the Spanish version of your strings.xml
there. Android will automatically decide which file to use depending on the configuration of the handset.
See also: