|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sji18n.I18n
public final class I18n
I18n
instances provide easy access to locale-specific objects and the class's static methods allow an
application to manage its locale configuration.
Support is provided to set a different locale for logging
messages and application objects. To use this feature, use the getLogString
or
getLogMessage
methods when generating log messages.
I18nListeners
can be
registered to listen for locale change events. This allows applications to implement dynamic locale switching.
In a more advanced (and perhaps more exotic) application, multiple locales can be set in separate parts of an
application runtime by introducing named contexts.
Every context can have its own locale. The default context is
the nameless context, i.e. its name is null
. A second predefined context is the default logging context
already mentioned above. The name of this context is "logging".
If required, applications can introduce and
manage more contexts.
Libraries that support multiple context management must have some classes (where
applicable) having a method to set an instance's context. These classes could implement I18nable
to
provide a uniform interface for setting an instance's context and/or logging context, but this is not a hard
requirement.
Implementation notes:
This class uses a L10nResourceFactory
implementation to find and load actual localization resources. The actual L10nResourceFactory
implementation used is set at this class's initialization. System property sji18n.L10nResourceFactory
,
if set, must contain the fully qualified class name of the implementation to load. Failing to load the class
specified in this system property will cause this class's initialization to fail.
If system property
sji18n.L10nResourceFactory
is not set, I18n will look for a L10nResourceFactory
service
provider (see ServiceLoader
for more info on configuring a service provider).
Setting system
property sji18n.resource.name.map
(since version 1.1) allows to refer to a file in which resource names
are mapped to other resource names. This allows to change the actual (and often hard-coded) resource names used to
construct I18n
instances at deploy time. It is even possible to map some or all resource names to the
same name.
The contents of this resource name map file must be formatted as follows:
sji18n.resource.name.map
must start with a forward slash to refer to a file relative to the
working directory or to refer to a resource in the classpath. If the value does not start with a forward slash
character, it must be a valid URL.
Field Summary | |
---|---|
static String |
LOGGING_CONTEXT
Context name of the default logging context. |
Constructor Summary | |
---|---|
I18n(String resourceName)
Creates a new instance obtaining its objects from a resource identified by the specified name. |
Method Summary | |
---|---|
static void |
addI18nListener(I18nListener listener)
Registers a I18nListener . |
Image |
getImage(String key)
Fetches an application object for the locale currently set for the default, nameless context and casts it to an Image . |
Image |
getImage(String context,
String key)
Fetches an application object for the locale currently set for the specified context and casts it to an Image . |
static Locale |
getLocale()
Returns the locale currently set for application objects in the default, nameless context. |
static Locale |
getLocale(String context)
Returns the locale currently set for application objects in the specified context. |
static Locale |
getLogLocale()
Returns the locale currently set for logging messages. |
String |
getLogMessage(String messageID,
Object... args)
Fetches a logging message string for the locale currently set for the default logging context and formats a message using the specified object arguments. |
String |
getLogString(String key)
Fetches a logging message string for the locale currently set for the default logging context. |
static String |
getMappedResourceName(String resourceName)
Returns the name the specified resource name is mapped to. |
String |
getMessage(String messageID,
Object... args)
Fetches an application string for the locale currently set for the default, nameless context and formats a message using the specified object arguments. |
String |
getMessage(String context,
String messageID,
Object... args)
Fetches an application string for the locale currently set for the specified context and formats a message using the specified object arguments. |
Object |
getObject(String key)
Fetches an application object for the locale currently set for the default, nameless context. |
Object |
getObject(String context,
String key)
Fetches an application object for the locale currently set for the specified context. |
String |
getString(String key)
Fetches an application string for the locale currently set for the default, nameless context. |
String |
getString(String context,
String key)
Fetches an application string for the locale currently set for the specified context. |
static void |
removeI18nListener(I18nListener listener)
Removed a I18nListener from the list of registered listeners. |
static void |
setAllLocales(Locale locale)
Sets the locale in all contexts (including the default, nameless context and the logging context). |
static void |
setLocale(Locale locale)
Sets the locale for application objects in the default, nameless context. |
static void |
setLocale(String context,
Locale locale)
Sets the locale for application objects in the specified context. |
static void |
setLogLocale(Locale locale)
Sets the locale for logging messages. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String LOGGING_CONTEXT
getLocale(LOGGING_CONTEXT)
,
setLocale(LOGGING_CONTEXT, locale)
or getString(LOGGING_CONTEXT, key)
all yield the
same result or effect as calling getLogLocale()
, setLogLocale(locale)
or
getLogString(key)
. The latter calls should return a little bit faster, however.
Constructor Detail |
---|
public I18n(String resourceName)
resourceName
- the name of the resource, should not be null.Method Detail |
---|
public static Locale getLocale()
Locale
instance, never null.public static Locale getLogLocale()
Locale
instance, never null.public static Locale getLocale(String context)
context
- name of the context to check the locale for, may be null to indicate the default, nameless
context.
Locale
instance, never null.public static void setLocale(Locale locale)
locale
- the locale to set, may be null to indicate Locale.getDefault()
.public static void setLogLocale(Locale locale)
locale
- the locale to set, may be null to indicate Locale.getDefault()
.public static void setLocale(String context, Locale locale)
context
- name of the context to set the locale for, null to indicate the default, nameless context.locale
- the locale to set, may be null to indicate Locale.getDefault()
.public static void setAllLocales(Locale locale)
locale
- the locale to set, may be null to indicate Locale.getDefault()
.public static void addI18nListener(I18nListener listener)
I18nListener
. Registered listeners receive events when the locale was changed in a
particular context.
listener
- the listener to register, may be null if none, in which case this method does nothing.public static void removeI18nListener(I18nListener listener)
I18nListener
from the list of registered listeners.
listener
- the listener to remove, may be null if none, in which case this method does nothing.public static String getMappedResourceName(String resourceName)
resourceName
- the resource name to lookup the mapped name for.
public String getString(String key)
getString(null, key)
would yield the same result but is less optimal as it requires to look up the
locale for the specified context. This method's implementation uses a direct reference to the default context's
locale to have a slightly improved performance.
key
- the key identifying the string to fetch, must not be null.
MissingResourceException
- when the specified key is not found.public String getMessage(String messageID, Object... args)
messageID
- the key identifying the message string to fetch, must not be null.args
- the objects to include in the formatted message.
MissingResourceException
- when the specified key is not found.public Object getObject(String key)
getObject(null, key)
would yield the same result but is less optimal as it requires to look up the
locale for the specified context. This method's implementation uses a direct reference to the default context's
locale to have a slightly improved performance.
key
- the key identifying the object to fetch, must not be null.
public Image getImage(String key)
Image
. Calling getImage(null, key)
would yield the same result but is less optimal as
it requires to look up the locale for the specified context. This method's implementation uses a direct reference
to the default context's locale to have a slightly improved performance.
key
- the key identifying the image to fetch, must not be null.
ClassCastException
- when the found object cannot be cast to an Image
.public String getString(String context, String key)
context
- the context in which the string is required, may be null to indicate the default, nameless
context.key
- the key identifying the string to fetch, must not be null.
MissingResourceException
- when the specified key is not found.public String getMessage(String context, String messageID, Object... args)
context
- the context in which the string is required, may be null to indicate the default, nameless
context.messageID
- the key identifying the message string to fetch, must not be null.args
- the objects to include in the formatted message.
MissingResourceException
- when the specified key is not found.public Object getObject(String context, String key)
context
- the context in which the object is required, may be null to indicate the default, nameless
context.key
- the key identifying the object to fetch, must not be null.
public Image getImage(String context, String key)
Image
.
context
- the context in which the image is required, may be null to indicate the default, nameless
context.key
- the key identifying the image to fetch, must not be null.
ClassCastException
- when the found object cannot be cast to an Image
.public String getLogString(String key)
getString(LOGGING_CONTEXT, key)
would yield the same result but is less optimal as it requires to
look up the locale for the specified context. This method's implementation uses a direct reference to the default
logging context's locale to have a slightly improved performance.
key
- the key identifying the string to fetch, must not be null.
MissingResourceException
- when the specified key is not found.public String getLogMessage(String messageID, Object... args)
messageID
- the key identifying the message string to fetch, must not be null.args
- the objects to include in the formatted message.
MissingResourceException
- when the specified key is not found.
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |