Package com.badlogic.gdx.utils
Class I18NBundle
- java.lang.Object
-
- com.badlogic.gdx.utils.I18NBundle
-
public class I18NBundle extends java.lang.Object
AI18NBundle
providesLocale
-specific resources loaded from property files. A bundle contains a number of named resources, whose names and values areStrings
. A bundle may have a parent bundle, and when a resource is not found in a bundle, the parent bundle is searched for the resource. If the fallback mechanism reaches the base bundle and still can't find the resource it throws aMissingResourceException
.- All bundles for the same group of resources share a common base bundle. This base bundle acts as the root and is the last fallback in case none of its children was able to respond to a request.
- The first level contains changes between different languages. Only the differences between a language and the language of
the base bundle need to be handled by a language-specific
I18NBundle
. - The second level contains changes between different countries that use the same language. Only the differences between a
country and the country of the language bundle need to be handled by a country-specific
I18NBundle
. - The third level contains changes that don't have a geographic reason (e.g. changes that where made at some point in time
like
PREEURO
where the currency of come countries changed. The country bundle would return the current currency (Euro) and thePREEURO
variant bundle would return the old currency (e.g. DM for Germany).
- BaseName (base bundle)
- BaseName_de (german language bundle)
- BaseName_fr (french language bundle)
- BaseName_de_DE (bundle with Germany specific resources in german)
- BaseName_de_CH (bundle with Switzerland specific resources in german)
- BaseName_fr_CH (bundle with Switzerland specific resources in french)
- BaseName_de_DE_PREEURO (bundle with Germany specific resources in german of the time before the Euro)
- BaseName_fr_FR_PREEURO (bundle with France specific resources in french of the time before the Euro)
- See Also:
PropertiesUtils
-
-
Constructor Summary
Constructors Constructor Description I18NBundle()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static I18NBundle
createBundle(FileHandle baseFileHandle)
Creates a new bundle using the specifiedbaseFileHandle
, the default locale and the default encoding "UTF-8".static I18NBundle
createBundle(FileHandle baseFileHandle, java.lang.String encoding)
Creates a new bundle using the specifiedbaseFileHandle
andencoding
; the default locale is used.static I18NBundle
createBundle(FileHandle baseFileHandle, java.util.Locale locale)
Creates a new bundle using the specifiedbaseFileHandle
andlocale
; the default encoding "UTF-8" is used.static I18NBundle
createBundle(FileHandle baseFileHandle, java.util.Locale locale, java.lang.String encoding)
Creates a new bundle using the specifiedbaseFileHandle
,locale
andencoding
.void
debug(java.lang.String placeholder)
Sets the value of all localized strings to String placeholder so hardcoded, unlocalized values can be easily spotted.java.lang.String
format(java.lang.String key, java.lang.Object... args)
Gets the string with the specified key from this bundle or one of its parent after replacing the given arguments if they occur.java.lang.String
get(java.lang.String key)
Gets a string for the given key from this bundle or one of its parents.static boolean
getExceptionOnMissingKey()
Returns the flag indicating whether to throw aMissingResourceException
from theget(key)
method if no string for the given key can be found.java.util.Locale
getLocale()
Returns the locale of this bundle.static boolean
getSimpleFormatter()
Returns the flag indicating whether to use the simplified message pattern syntax (default is false).java.util.Set<java.lang.String>
keys()
Gets a key set of loaded properties.protected void
load(java.io.Reader reader)
Load the properties from the specified reader.static void
setExceptionOnMissingKey(boolean enabled)
Sets the flag indicating whether to throw aMissingResourceException
from theget(key)
method if no string for the given key can be found.static void
setSimpleFormatter(boolean enabled)
Sets the flag indicating whether to use the simplified message pattern.
-
-
-
Method Detail
-
getSimpleFormatter
public static boolean getSimpleFormatter()
Returns the flag indicating whether to use the simplified message pattern syntax (default is false). This flag is always assumed to be true on GWT backend.
-
setSimpleFormatter
public static void setSimpleFormatter(boolean enabled)
Sets the flag indicating whether to use the simplified message pattern. The flag must be set before calling the factory methodscreateBundle
. Notice that this method has no effect on the GWT backend where it's always assumed to be true.
-
getExceptionOnMissingKey
public static boolean getExceptionOnMissingKey()
Returns the flag indicating whether to throw aMissingResourceException
from theget(key)
method if no string for the given key can be found. If this flag isfalse
the missing key surrounded by???
is returned.
-
setExceptionOnMissingKey
public static void setExceptionOnMissingKey(boolean enabled)
Sets the flag indicating whether to throw aMissingResourceException
from theget(key)
method if no string for the given key can be found. If this flag isfalse
the missing key surrounded by???
is returned.
-
createBundle
public static I18NBundle createBundle(FileHandle baseFileHandle)
Creates a new bundle using the specifiedbaseFileHandle
, the default locale and the default encoding "UTF-8".- Parameters:
baseFileHandle
- the file handle to the base of the bundle- Returns:
- a bundle for the given base file handle and the default locale
- Throws:
java.lang.NullPointerException
- ifbaseFileHandle
isnull
java.util.MissingResourceException
- if no bundle for the specified base file handle can be found
-
createBundle
public static I18NBundle createBundle(FileHandle baseFileHandle, java.util.Locale locale)
Creates a new bundle using the specifiedbaseFileHandle
andlocale
; the default encoding "UTF-8" is used.- Parameters:
baseFileHandle
- the file handle to the base of the bundlelocale
- the locale for which a bundle is desired- Returns:
- a bundle for the given base file handle and locale
- Throws:
java.lang.NullPointerException
- ifbaseFileHandle
orlocale
isnull
java.util.MissingResourceException
- if no bundle for the specified base file handle can be found
-
createBundle
public static I18NBundle createBundle(FileHandle baseFileHandle, java.lang.String encoding)
Creates a new bundle using the specifiedbaseFileHandle
andencoding
; the default locale is used.- Parameters:
baseFileHandle
- the file handle to the base of the bundleencoding
- the character encoding- Returns:
- a bundle for the given base file handle and locale
- Throws:
java.lang.NullPointerException
- ifbaseFileHandle
orencoding
isnull
java.util.MissingResourceException
- if no bundle for the specified base file handle can be found
-
createBundle
public static I18NBundle createBundle(FileHandle baseFileHandle, java.util.Locale locale, java.lang.String encoding)
Creates a new bundle using the specifiedbaseFileHandle
,locale
andencoding
.- Parameters:
baseFileHandle
- the file handle to the base of the bundlelocale
- the locale for which a bundle is desiredencoding
- the character encoding- Returns:
- a bundle for the given base file handle and locale
- Throws:
java.lang.NullPointerException
- ifbaseFileHandle
,locale
orencoding
isnull
java.util.MissingResourceException
- if no bundle for the specified base file handle can be found
-
load
protected void load(java.io.Reader reader) throws java.io.IOException
Load the properties from the specified reader.- Parameters:
reader
- the reader- Throws:
java.io.IOException
- if an error occurred when reading from the input stream.
-
getLocale
public java.util.Locale getLocale()
Returns the locale of this bundle. This method can be used after a call tocreateBundle()
to determine whether the resource bundle returned really corresponds to the requested locale or is a fallback.- Returns:
- the locale of this bundle
-
get
public java.lang.String get(java.lang.String key)
Gets a string for the given key from this bundle or one of its parents.- Parameters:
key
- the key for the desired string- Returns:
- the string for the given key or the key surrounded by
???
if it cannot be found andgetExceptionOnMissingKey()
returnsfalse
- Throws:
java.lang.NullPointerException
- ifkey
isnull
java.util.MissingResourceException
- if no string for the given key can be found andgetExceptionOnMissingKey()
returnstrue
-
keys
public java.util.Set<java.lang.String> keys()
Gets a key set of loaded properties. Keys will be copied into a new set and returned.- Returns:
- a key set of loaded properties. Never null, might be an empty set
-
format
public java.lang.String format(java.lang.String key, java.lang.Object... args)
Gets the string with the specified key from this bundle or one of its parent after replacing the given arguments if they occur.- Parameters:
key
- the key for the desired stringargs
- the arguments to be replaced in the string associated to the given key.- Returns:
- the string for the given key formatted with the given arguments
- Throws:
java.lang.NullPointerException
- ifkey
isnull
java.util.MissingResourceException
- if no string for the given key can be found
-
debug
public void debug(java.lang.String placeholder)
Sets the value of all localized strings to String placeholder so hardcoded, unlocalized values can be easily spotted. The I18NBundle won't be able to reset values after calling debug and should only be using during testing.- Parameters:
placeholder
-
-
-