Package org.codehaus.plexus.i18n
Class DefaultI18N
- java.lang.Object
-
- org.codehaus.plexus.logging.AbstractLogEnabled
-
- org.codehaus.plexus.i18n.DefaultI18N
-
- All Implemented Interfaces:
I18N
,org.codehaus.plexus.logging.LogEnabled
,org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable
public class DefaultI18N extends org.codehaus.plexus.logging.AbstractLogEnabled implements I18N, org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
bundleNames
private java.util.HashMap
bundles
private java.lang.String
defaultBundleName
private java.lang.String
defaultCountry
private java.lang.String
defaultLanguage
private java.util.Locale
defaultLocale
private boolean
devMode
private static java.lang.Object[]
NO_ARGS
-
Fields inherited from interface org.codehaus.plexus.i18n.I18N
ACCEPT_LANGUAGE, ROLE
-
-
Constructor Summary
Constructors Constructor Description DefaultI18N()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.ResourceBundle
cacheBundle(java.lang.String bundleName, java.util.Locale locale)
Caches the named bundle for fast lookups.private java.util.ResourceBundle
findBundleByLocale(java.lang.String bundleName, java.util.Locale locale, java.util.Map bundlesByLocale)
Retrieves the bundle most closely matching first against the supplied inputs, then against the defaults.java.lang.String
format(java.lang.String key, java.lang.Object arg1)
java.lang.String
format(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
java.lang.String
format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1)
java.lang.String
format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object[] args)
Looks up the value forkey
in theResourceBundle
referenced bybundleName
, then formats that value for the specifiedLocale
usingargs
.java.lang.String
format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
java.util.ResourceBundle
getBundle()
java.util.ResourceBundle
getBundle(java.lang.String bundleName)
java.util.ResourceBundle
getBundle(java.lang.String bundleName, java.lang.String languageHeader)
This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header.java.util.ResourceBundle
getBundle(java.lang.String bundleName, java.util.Locale locale)
This method returns a ResourceBundle for the given bundle name and the given Locale.private java.util.ResourceBundle
getBundleIgnoreException(java.lang.String bundleName, java.util.Locale locale)
Retrieves the bundle using theResourceBundle.getBundle(String, Locale)
method, returningnull
instead of throwingMissingResourceException
.java.lang.String[]
getBundleNames()
java.lang.String
getDefaultBundleName()
java.lang.String
getDefaultCountry()
java.lang.String
getDefaultLanguage()
java.util.Locale
getLocale(java.lang.String header)
java.lang.String
getString(java.lang.String key)
java.lang.String
getString(java.lang.String key, java.util.Locale locale)
java.lang.String
getString(java.lang.String bundleName, java.util.Locale locale, java.lang.String key)
protected java.lang.String
getStringOrNull(java.util.ResourceBundle rb, java.lang.String key)
Gets localized text from a bundle if it's there.void
initialize()
Called the first time the Service is used.protected void
initializeBundleNames()
-
-
-
Field Detail
-
NO_ARGS
private static final java.lang.Object[] NO_ARGS
-
bundles
private java.util.HashMap bundles
-
bundleNames
private java.lang.String[] bundleNames
-
defaultBundleName
private java.lang.String defaultBundleName
-
defaultLocale
private java.util.Locale defaultLocale
-
defaultLanguage
private java.lang.String defaultLanguage
-
defaultCountry
private java.lang.String defaultCountry
-
devMode
private boolean devMode
-
-
Method Detail
-
getDefaultLanguage
public java.lang.String getDefaultLanguage()
- Specified by:
getDefaultLanguage
in interfaceI18N
-
getDefaultCountry
public java.lang.String getDefaultCountry()
- Specified by:
getDefaultCountry
in interfaceI18N
-
getDefaultBundleName
public java.lang.String getDefaultBundleName()
- Specified by:
getDefaultBundleName
in interfaceI18N
-
getBundleNames
public java.lang.String[] getBundleNames()
- Specified by:
getBundleNames
in interfaceI18N
-
getBundle
public java.util.ResourceBundle getBundle(java.lang.String bundleName)
-
getBundle
public java.util.ResourceBundle getBundle(java.lang.String bundleName, java.lang.String languageHeader)
This method returns a ResourceBundle given the bundle name and the Locale information supplied in the HTTP "Accept-Language" header.
-
getBundle
public java.util.ResourceBundle getBundle(java.lang.String bundleName, java.util.Locale locale)
This method returns a ResourceBundle for the given bundle name and the given Locale.
-
getLocale
public java.util.Locale getLocale(java.lang.String header)
- Specified by:
getLocale
in interfaceI18N
- See Also:
I18N.getLocale(String)
-
getString
public java.lang.String getString(java.lang.String key)
-
getString
public java.lang.String getString(java.lang.String key, java.util.Locale locale)
-
getString
public java.lang.String getString(java.lang.String bundleName, java.util.Locale locale, java.lang.String key)
- Specified by:
getString
in interfaceI18N
- Throws:
java.util.MissingResourceException
- Specified key cannot be matched.- See Also:
I18N.getString(String, Locale, String)
-
format
public java.lang.String format(java.lang.String key, java.lang.Object arg1)
-
format
public java.lang.String format(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
-
format
public java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1)
- Specified by:
format
in interfaceI18N
- See Also:
I18N.format(String, Locale, String, Object)
-
format
public java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
- Specified by:
format
in interfaceI18N
- See Also:
I18N.format(String, Locale, String, Object, Object)
-
format
public java.lang.String format(java.lang.String bundleName, java.util.Locale locale, java.lang.String key, java.lang.Object[] args)
Looks up the value forkey
in theResourceBundle
referenced bybundleName
, then formats that value for the specifiedLocale
usingargs
.
-
initialize
public void initialize() throws org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException
Called the first time the Service is used.- Specified by:
initialize
in interfaceorg.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable
- Throws:
org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException
-
initializeBundleNames
protected void initializeBundleNames()
-
cacheBundle
private java.util.ResourceBundle cacheBundle(java.lang.String bundleName, java.util.Locale locale) throws java.util.MissingResourceException
Caches the named bundle for fast lookups. This operation is relatively expesive in terms of memory use, but is optimized for run-time speed in the usual case.- Throws:
java.util.MissingResourceException
- Bundle not found.
-
findBundleByLocale
private java.util.ResourceBundle findBundleByLocale(java.lang.String bundleName, java.util.Locale locale, java.util.Map bundlesByLocale)
Retrieves the bundle most closely matching first against the supplied inputs, then against the defaults.
Use case: some clients send a HTTP Accept-Language header with a value of only the language to use (i.e. "Accept-Language: en"), and neglect to include a country. When there is no bundle for the requested language, this method can be called to try the default country (checking internally to assure the requested criteria matches the default to avoid disconnects between language and country).
Since we're really just guessing at possible bundles to use, we don't ever throw
MissingResourceException
.
-
getBundleIgnoreException
private java.util.ResourceBundle getBundleIgnoreException(java.lang.String bundleName, java.util.Locale locale)
Retrieves the bundle using theResourceBundle.getBundle(String, Locale)
method, returningnull
instead of throwingMissingResourceException
.
-
getStringOrNull
protected final java.lang.String getStringOrNull(java.util.ResourceBundle rb, java.lang.String key)
Gets localized text from a bundle if it's there. Otherwise, returnsnull
(ignoring a possibleMissingResourceException
).
-
-