java.lang.Object
org.apache.wicket.Localizer
A utility class that encapsulates all of the localization related functionality in a way that it
can be accessed by all areas of the framework in a consistent way. A singleton instance of this
class is available via the
Application
object.
You may register additional IStringResourceLoader to extend or replace Wickets default search strategy for the properties. E.g. string resource loaders which load the properties from a database. There should be hardly any need to extend Localizer.
- Author:
- Chris Turner, Juergen Donnerstag
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate the utils instance class backed by the configuration information contained within the supplied application object. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Clear all cache entries by instantiating a new cache objectstatic Localizer
get()
protected String
Gets the cache keyprotected String
getFromCache
(String cacheKey) Get the value associated with the key from the cache.getString
(String key, Component component, IModel<?> model, Locale locale, String style, String defaultValue) Get the localized string using all of the supplied parameters.getString
(String key, Component component, IModel<?> model, Locale locale, String style, IModel<String> defaultValue) Get the localized string using all of the supplied parameters.getStringIgnoreSettings
(String key, Component component, IModel<?> model, String defaultValue) getStringIgnoreSettings
(String key, Component component, IModel<?> model, Locale locale, String style, String defaultValue) This is similar togetString(String, Component, IModel, String)
except that the resource settings are ignored.protected List<IStringResourceLoader>
In case you want to provide your own list of string resource loadersnewCache()
Create a new cache, override this method if you want a different map to store the cache keys, for example a map that hold only the last X number of elements..protected void
putIntoCache
(String cacheKey, String string) Put the value into the cache and associate it with the cache keyfinal void
setEnableCache
(boolean value) By default the cache is enabled.substitutePropertyExpressions
(Component component, String string, IModel<?> model) Helper method to handle property variable substitution in strings.
-
Constructor Details
-
Localizer
public Localizer()Create the utils instance class backed by the configuration information contained within the supplied application object.
-
-
Method Details
-
get
- Returns:
- Same as Application.get().getResourceSettings().getLocalizer()
-
clearCache
Clear all cache entries by instantiating a new cache object- See Also:
-
getString
- Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource for- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown- See Also:
-
getString
public String getString(String key, Component component, IModel<?> model) throws MissingResourceException - Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource formodel
- The model to use for property substitutions in the strings (optional)- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown- See Also:
-
getString
public String getString(String key, Component component, String defaultValue) throws MissingResourceException - Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource fordefaultValue
- The default value (optional)- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown- See Also:
-
getString
public String getString(String key, Component component, IModel<?> model, String defaultValue) throws MissingResourceException - Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource formodel
- The model to use for property substitutions in the strings (optional)defaultValue
- The default value (optional)- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown- See Also:
-
getString
public String getString(String key, Component component, IModel<?> model, Locale locale, String style, String defaultValue) throws MissingResourceException Get the localized string using all of the supplied parameters. This method is left public to allow developers full control over string resource loading. However, it is recommended that one of the other convenience methods in the class are used as they handle all of the work related to obtaining the current user locale and style information.- Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource for (optional)model
- The model to use for substitutions in the strings (optional)locale
- If != null, it'll supersede the component's localestyle
- If != null, it'll supersede the component's styledefaultValue
- The default value (optional)- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown
-
getString
public String getString(String key, Component component, IModel<?> model, Locale locale, String style, IModel<String> defaultValue) throws MissingResourceException Get the localized string using all of the supplied parameters. This method is left public to allow developers full control over string resource loading. However, it is recommended that one of the other convenience methods in the class are used as they handle all of the work related to obtaining the current user locale and style information.- Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource for (optional)model
- The model to use for substitutions in the strings (optional)locale
- If != null, it'll supersede the component's localestyle
- If != null, it'll supersede the component's styledefaultValue
- The default value (optional)- Returns:
- The string resource
- Throws:
MissingResourceException
- If resource not found and configuration dictates that exception should be thrown
-
getStringIgnoreSettings
public String getStringIgnoreSettings(String key, Component component, IModel<?> model, String defaultValue) - Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource for (optional)model
- The model to use for substitutions in the strings (optional)defaultValue
- The default value (optional)- Returns:
- The string resource
- See Also:
-
getStringIgnoreSettings
public String getStringIgnoreSettings(String key, Component component, IModel<?> model, Locale locale, String style, String defaultValue) This is similar togetString(String, Component, IModel, String)
except that the resource settings are ignored. This allows to to code something likeString option = getLocalizer().getStringIgnoreSettings(getId() + ".null", this, ""); if (Strings.isEmpty(option)) { option = getLocalizer().getString("null", this, CHOOSE_ONE); }
- Parameters:
key
- The key to obtain the resource forcomponent
- The component to get the resource for (optional)model
- The model to use for substitutions in the strings (optional)locale
- If != null, it'll supersede the component's localestyle
- If != null, it'll supersede the component's styledefaultValue
- The default value (optional)- Returns:
- The string resource
-
getStringResourceLoaders
In case you want to provide your own list of string resource loaders- Returns:
- List of string resource loaders
-
putIntoCache
Put the value into the cache and associate it with the cache key- Parameters:
cacheKey
-string
-
-
getFromCache
Get the value associated with the key from the cache.- Parameters:
cacheKey
-- Returns:
- The value of the key
-
getCacheKey
protected String getCacheKey(String key, Component component, Locale locale, String style, String variation) Gets the cache key- Parameters:
key
-component
-locale
- Guaranteed to be != nullstyle
-variation
-- Returns:
- The value of the key
-
substitutePropertyExpressions
Helper method to handle property variable substitution in strings.- Parameters:
component
- The component requesting a model value ornull
string
- The string to substitute intomodel
- The model- Returns:
- The resulting string
-
setEnableCache
By default the cache is enabled. Disabling the cache will disable it and clear the cache. This can be handy for example in development mode.- Parameters:
value
-
-
newCache
Create a new cache, override this method if you want a different map to store the cache keys, for example a map that hold only the last X number of elements.. By default it uses theConcurrentHashMap
- Returns:
- cache
-