MessageBundle
From Gardenwiki
GameGardens projects use various MessageBundle utilities in many places. If you get an error message saying that a translation message does not exist, or if you see "m.something_or_other" where you expected words that made sense, then the following should be useful to you in fixing it.
Contents |
Define Messages
Each message used needs to be defined in the properties file. Look in the i18n folder inside the rsrc folder. You will see lines such as:
m.range_type = Difficulty
m.toggle_use_letters = Use letters?
m.choice_difficultyC = Difficulty:
m.choice_Easy = Easy
m.word_accepted = You scored {1} for '{0}'.
m.result = {0}
The right hand side of each line gives the actual text to be displayed if there are no {}. Each set of {} is replaced by the appropriate parameter passed to the MessageBundle utility. For now, just assume that any {0} or {1} will be replaced by a String later.
You will need one line for each range, toggle, or choice parameter in the lobby, and another line for each of the various choices that can be made by your choice parameters in the lobby.
Using Messages in the Lobby
In the <params> section of the XML file for your game, you will define the various parameters you want to show. For example:
<choice ident="difficultyC" choices="Easy,Medium,Hard,Insane" start="Easy"/> <choice ident="language" choices="Pirate,English" start="Pirate"/> <toggle ident="unmixed" start="false"/> <range ident="penalty" minimum="1" maximum="20" start="1"/>
Each of the idents requires a line in the properties file. Each of the choices requires a line as well. For a range, the message to define will be "m.range_" and the ident string. Similarly for a toggle, use "m.toggle_" and the ident string. On the right hand side of the line, put the exact text that should appear in the lobby for that control.
Using Messages from Java
A message bundle provides an easy mechanism by which to obtain translated message strings from a resource bundle. It uses the MessageFormat class to substitute arguments into the translation strings.
In the MummichogPanel class, we find several examples of the use of a message to be translated. For example:
sidePanel.add(_status = new JLabel(_msgs.get("m.waiting_for_start")), VGroupLayout.FIXED);
JButton back = new JButton(_msgs.get("m.back_to_lobby"));
_status.setText(_msgs.get("m.go"));
_status.setText(_msgs.xlate(status));
_msgs is a MessageBundle variable. get() returns a string that is the translation for the specified message key. No arguments are substituted into the translated string. xlate() returns the translation for the specified compound message key.
The Chat Window Messages tutorial gives two examples of using the tcompose() method. Here is one:
String msg = "" + _myobj.score;
msg = MessageBundle.tcompose("m.game_over_score", msg);
systemMessage(MummichogCodes.MUMMICHOG_MSG_BUNDLE, msg);
tcompose() returns a compound message key that can later be translated using the xlate() method.
If you have relevant experience with MessageBundle, please expand this section.
general.properties
There is a strangeness in the GameGardens code when a player leaves a table after the game starts but before the game finishes. The translation code for that event needs to be in a different properties file. Make a folder inside the i18n folder, named game, and add a textfile named general.properties with the following line in it:
m.player_game_over = {0} has left the game.
This should patch this bug.
Integer Handling
One nice feature, or meddlesome quirk, of the MessageBundle is how it handles Integers. Here's what the MessageBundle.get() JavaDocs have to say:
public String get(String key, Object... args)
...
If the first argument in the array is an Integer object,
a translation will be selected accounting for plurality in the following manner.
Assume a message key of m.widgets, the following translations should be defined:
m.widgets.0 = no widgets.
m.widgets.1 = {0} widget.
m.widgets.n = {0} widgets.
...
If you don't want different messages, you can avoid this by turning your Integer into a String:
String text=_msgs.get("m.final_score",Integer.toString(score));

