net.sf.freecol.client.gui.i18n
Class Messages

java.lang.Object
  extended by net.sf.freecol.client.gui.i18n.Messages

public class Messages
extends java.lang.Object

Represents a collection of messages in a particular locale.

The individual messages are read from property files in the data/strings directory. The property files are called "FreeColMessages[_LANGUAGE[_COUNTRY[_VARIANT]]].properties", where LANGUAGE should be an ISO 639-2 or ISO 639-3 language code, COUNTRY should be an ISO 3166-2 country code, and VARIANT is an arbitrary string. The encoding of the property files is UTF-8. Since the Java Properties class is unable to handle UTF-8 directly, this class uses its own implementation.

The individual messages may include variables, which must be delimited by percent characters (e.g. "%nation%"), and will be replaced when the message is formatted. Furthermore, the messages may include choice formats consisting of a tag followed by a colon (":"), a selector and one or several choices separated from the selector and each other by pipe characters ("|"). The entire choice format must be enclosed in double brackets ("{{" and "}}", respectively).

Each choice must consist of a key and a value separated by an equals character ("="), unless it is a variable, in which case the variable must resolve to another choice format. The selector may also be a variable. If the selector is omitted, then one of the choices should use the key "default". Choice formats may be nested.

   key1=%colony% tuottaa tuotetta {{tag:acc|%goods%}}.
   key2={{plural:%amount%|one=ruoka|other=ruokaa|default={{tag:|acc=viljaa|default=Vilja}}}}
   key3={{tag:|acc=viljaa|default={{plural:%amount%|one=ruoka|other=ruokaa|default=Ruoka}}}}
 

This class is NOT thread-safe. (CO: I cannot find any place that really has a problem)


Field Summary
private static java.lang.String[] DESCRIPTION_KEYS
           
static java.lang.String FILE_PREFIX
           
static java.lang.String FILE_SUFFIX
           
private static java.util.logging.Logger logger
           
private static java.util.Map<java.lang.String,java.lang.String> messageBundle
           
static java.lang.String STRINGS_DIRECTORY
           
private static java.util.Map<java.lang.String,Selector> tagMap
          A map with Selector values and the tag keys used in choice formats.
 
Constructor Summary
Messages()
           
 
Method Summary
static boolean containsKey(java.lang.String key)
          Returns true if the message bundle contains the given key.
private static int findMatchingBracket(java.lang.String input, int start)
          Return the index of the matching pair of brackets, or -1 if none is found.
static int getBreakingPoint(java.lang.String string)
          Breaks a line between two words.
private static java.lang.String getChoice(java.lang.String input, java.lang.String key)
          Return the choice tagged with the given key, or null, if the given input string does not contain the key.
static java.lang.String getDefaultRegionName(Player player, Region.RegionType regionType)
          Creates a unique region name by fetching a new default name from the list of default names if possible.
static java.lang.String getDescription(FreeColObject object)
           
static java.lang.String getDescription(Option object)
           
static java.io.File getI18nDirectory()
          Returns the directory containing language property files.
static java.lang.String getKey(java.lang.String preferredKey, java.lang.String defaultKey)
          Returns the preferred key if it is contained in the message bundle and the default key otherwise.
static java.lang.String getLabel(AbstractUnit unit)
          Returns the name of a unit in a human readable format.
static StringTemplate getLabel(Unit unit)
          Returns the name of a unit in a human readable format.
static java.lang.String getLabel(UnitType someType, Unit.Role someRole, int count)
          Returns the name of a unit in a human readable format.
static java.lang.String getName(FreeColObject object)
           
static java.lang.String getName(Option object)
           
static java.lang.String getNewLandName(Player player)
           
private static Selector getSelector(java.lang.String tag)
          Returns the Selector with the given tag.
static java.util.List<java.lang.String> getSettlementNames(Player player)
          Gets a list of settlement names and a fallback prefix for a player.
static java.lang.String getShortDescription(FreeColObject object)
           
static java.lang.String getShortDescription(Option object)
           
static java.lang.String getStanceAsString(Player.Stance stance)
          Returns a string describing the given stance.
static java.lang.String getTurnsText(int turns)
          Gets a string describing the number of turns left for a colony to finish building something.
static void loadResources(java.io.File resourceFile)
          Loads a new resource file into the current message bundle.
static void loadResources(java.io.InputStream is)
          Loads a new resource file into the current message bundle.
static java.lang.String message(java.lang.String messageId)
          Returns the text mapping for a particular ID in the default locale message bundle.
static java.lang.String message(StringTemplate template)
          Localizes a StringTemplate.
private static java.lang.String replaceChoices(java.lang.String input, StringTemplate template)
          Replace all choice formats in the given string, using keys and replacement values from the given template, which may be null.
static void setGrammaticalNumber(Number number)
          Set the grammatical number rule.
static void setMessageBundle(java.util.Locale locale)
          Set the resource bundle for the given locale
private static void setMessageBundle(java.lang.String language, java.lang.String country, java.lang.String variant)
          Set the resource bundle to the given locale
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final java.util.logging.Logger logger

STRINGS_DIRECTORY

public static final java.lang.String STRINGS_DIRECTORY
See Also:
Constant Field Values

FILE_PREFIX

public static final java.lang.String FILE_PREFIX
See Also:
Constant Field Values

FILE_SUFFIX

public static final java.lang.String FILE_SUFFIX
See Also:
Constant Field Values

DESCRIPTION_KEYS

private static final java.lang.String[] DESCRIPTION_KEYS

messageBundle

private static java.util.Map<java.lang.String,java.lang.String> messageBundle

tagMap

private static java.util.Map<java.lang.String,Selector> tagMap
A map with Selector values and the tag keys used in choice formats.

Constructor Detail

Messages

public Messages()
Method Detail

getSelector

private static Selector getSelector(java.lang.String tag)
Returns the Selector with the given tag.

Parameters:
tag - a String value
Returns:
a Selector value

setGrammaticalNumber

public static void setGrammaticalNumber(Number number)
Set the grammatical number rule.

Parameters:
number - a Number value

setMessageBundle

public static void setMessageBundle(java.util.Locale locale)
Set the resource bundle for the given locale

Parameters:
locale -

setMessageBundle

private static void setMessageBundle(java.lang.String language,
                                     java.lang.String country,
                                     java.lang.String variant)
Set the resource bundle to the given locale

Parameters:
language - The language for this locale.
country - The language for this locale.
variant - The variant for this locale.

getI18nDirectory

public static java.io.File getI18nDirectory()
Returns the directory containing language property files.

Returns:
a File value

message

public static java.lang.String message(java.lang.String messageId)
Returns the text mapping for a particular ID in the default locale message bundle. Returns the key as the value if there is no mapping found!

Parameters:
messageId - The key of the message to find
Returns:
String text mapping or the key

replaceChoices

private static java.lang.String replaceChoices(java.lang.String input,
                                               StringTemplate template)
Replace all choice formats in the given string, using keys and replacement values from the given template, which may be null. A choice format is enclosed in double brackets and consists of a tag, followed by a colon, followed by an optional selector, followed by a pipe character, followed by one or several choices separated by pipe characters. If there is only one choice, it must be a message id or a variable. Otherwise, each choice consists of a key and a value separated by an assignment character. Example: "{{tag:selector|key1=val1|key2=val2}}".

Parameters:
input - a String value
template - a StringTemplate value
Returns:
a String value

getChoice

private static java.lang.String getChoice(java.lang.String input,
                                          java.lang.String key)
Return the choice tagged with the given key, or null, if the given input string does not contain the key.

Parameters:
input - a String value
key - a String value
Returns:
a String value

findMatchingBracket

private static int findMatchingBracket(java.lang.String input,
                                       int start)
Return the index of the matching pair of brackets, or -1 if none is found.

Parameters:
input - a String value
start - an int value
Returns:
an int value

message

public static java.lang.String message(StringTemplate template)
Localizes a StringTemplate.

Parameters:
template - a StringTemplate value
Returns:
a String value

containsKey

public static boolean containsKey(java.lang.String key)
Returns true if the message bundle contains the given key.

Parameters:
key - a String value
Returns:
a boolean value

getKey

public static java.lang.String getKey(java.lang.String preferredKey,
                                      java.lang.String defaultKey)
Returns the preferred key if it is contained in the message bundle and the default key otherwise. This should be used to select the most specific message key available.

Parameters:
preferredKey - a String value
defaultKey - a String value
Returns:
a String value

getName

public static java.lang.String getName(FreeColObject object)

getDescription

public static java.lang.String getDescription(FreeColObject object)

getShortDescription

public static java.lang.String getShortDescription(FreeColObject object)

getName

public static java.lang.String getName(Option object)

getDescription

public static java.lang.String getDescription(Option object)

getShortDescription

public static java.lang.String getShortDescription(Option object)

getLabel

public static StringTemplate getLabel(Unit unit)
Returns the name of a unit in a human readable format. The label consists of up to three items: If the unit has a role other than the unit type's default role, the current role, the proper name of the unit and the unit's type. Otherwise, the unit's type, the proper name of the unit, and additional information about gold (in the case of treasure trains), or equipment.

Parameters:
unit - an Unit value
Returns:
A label to describe the given unit

getLabel

public static java.lang.String getLabel(UnitType someType,
                                        Unit.Role someRole,
                                        int count)
Returns the name of a unit in a human readable format. The return value can be used when communicating with the user.

Parameters:
someType - an UnitType value
someRole - a Role value
count - an int value
Returns:
The given unit type as a String

getLabel

public static java.lang.String getLabel(AbstractUnit unit)
Returns the name of a unit in a human readable format. The return value can be used when communicating with the user.

Parameters:
unit - an AbstractUnit value
Returns:
The given unit type as a String

getStanceAsString

public static java.lang.String getStanceAsString(Player.Stance stance)
Returns a string describing the given stance.

Parameters:
stance - The stance.
Returns:
A matching string.

getTurnsText

public static java.lang.String getTurnsText(int turns)
Gets a string describing the number of turns left for a colony to finish building something.

Parameters:
turns - the number of turns left
Returns:
A descriptive string.

getNewLandName

public static java.lang.String getNewLandName(Player player)

getDefaultRegionName

public static java.lang.String getDefaultRegionName(Player player,
                                                    Region.RegionType regionType)
Creates a unique region name by fetching a new default name from the list of default names if possible.

Parameters:
player - Player
regionType - a RegionType value
Returns:
a String value

getSettlementNames

public static java.util.List<java.lang.String> getSettlementNames(Player player)
Gets a list of settlement names and a fallback prefix for a player.

Parameters:
player - The Player to get names for.
Returns:
A list of settlement names, with the first being the fallback prefix.

loadResources

public static void loadResources(java.io.File resourceFile)
Loads a new resource file into the current message bundle.

Parameters:
resourceFile -

loadResources

public static void loadResources(java.io.InputStream is)
Loads a new resource file into the current message bundle.

Parameters:
is - an InputStream value

getBreakingPoint

public static int getBreakingPoint(java.lang.String string)
Breaks a line between two words. The breaking point is as close to the center as possible.

Parameters:
string - The line for which we should determine a breaking point.
Returns:
The best breaking point or -1 if there are none.