Class Localizer


  • public class Localizer
    extends java.lang.Object
    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:
    ResourceSettings.getLocalizer(), IStringResourceLoader, ResourceSettings.getStringResourceLoaders()
    • Constructor Summary

      Constructors 
      Constructor Description
      Localizer()
      Create the utils instance class backed by the configuration information contained within the supplied application object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearCache()
      Clear all cache entries by instantiating a new cache object
      static Localizer get()  
      protected java.lang.String getCacheKey​(java.lang.String key, Component component, java.util.Locale locale, java.lang.String style, java.lang.String variation)
      Gets the cache key
      protected java.lang.String getFromCache​(java.lang.String cacheKey)
      Get the value associated with the key from the cache.
      java.lang.String getString​(java.lang.String key, Component component)  
      java.lang.String getString​(java.lang.String key, Component component, java.lang.String defaultValue)  
      java.lang.String getString​(java.lang.String key, Component component, IModel<?> model)  
      java.lang.String getString​(java.lang.String key, Component component, IModel<?> model, java.lang.String defaultValue)  
      java.lang.String getString​(java.lang.String key, Component component, IModel<?> model, java.util.Locale locale, java.lang.String style, java.lang.String defaultValue)
      Get the localized string using all of the supplied parameters.
      java.lang.String getString​(java.lang.String key, Component component, IModel<?> model, java.util.Locale locale, java.lang.String style, IModel<java.lang.String> defaultValue)
      Get the localized string using all of the supplied parameters.
      java.lang.String getStringIgnoreSettings​(java.lang.String key, Component component, IModel<?> model, java.lang.String defaultValue)  
      java.lang.String getStringIgnoreSettings​(java.lang.String key, Component component, IModel<?> model, java.util.Locale locale, java.lang.String style, java.lang.String defaultValue)
      This is similar to getString(String, Component, IModel, String) except that the resource settings are ignored.
      protected java.util.List<IStringResourceLoader> getStringResourceLoaders()
      In case you want to provide your own list of string resource loaders
      protected java.util.Map<java.lang.String,​java.lang.String> 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..
      protected void putIntoCache​(java.lang.String cacheKey, java.lang.String string)
      Put the value into the cache and associate it with the cache key
      void setEnableCache​(boolean value)
      By default the cache is enabled.
      java.lang.String substitutePropertyExpressions​(Component component, java.lang.String string, IModel<?> model)
      Helper method to handle property variable substitution in strings.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Localizer

        public Localizer()
        Create the utils instance class backed by the configuration information contained within the supplied application object.
    • Method Detail

      • get

        public static Localizer get()
        Returns:
        Same as Application.get().getResourceSettings().getLocalizer()
      • clearCache

        public final void clearCache()
        Clear all cache entries by instantiating a new cache object
        See Also:
        newCache()
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component)
                                   throws java.util.MissingResourceException
        Parameters:
        key - The key to obtain the resource for
        component - The component to get the resource for
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
        See Also:
        getString(String, Component, IModel, Locale, String, String)
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component,
                                          IModel<?> model)
                                   throws java.util.MissingResourceException
        Parameters:
        key - The key to obtain the resource for
        component - The component to get the resource for
        model - The model to use for property substitutions in the strings (optional)
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
        See Also:
        getString(String, Component, IModel, Locale, String, String)
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component,
                                          java.lang.String defaultValue)
                                   throws java.util.MissingResourceException
        Parameters:
        key - The key to obtain the resource for
        component - The component to get the resource for
        defaultValue - The default value (optional)
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
        See Also:
        getString(String, Component, IModel, Locale, String, String)
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component,
                                          IModel<?> model,
                                          java.lang.String defaultValue)
                                   throws java.util.MissingResourceException
        Parameters:
        key - The key to obtain the resource for
        component - The component to get the resource for
        model - The model to use for property substitutions in the strings (optional)
        defaultValue - The default value (optional)
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
        See Also:
        getString(String, Component, IModel, Locale, String, String)
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component,
                                          IModel<?> model,
                                          java.util.Locale locale,
                                          java.lang.String style,
                                          java.lang.String defaultValue)
                                   throws java.util.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 for
        component - 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 locale
        style - If != null, it'll supersede the component's style
        defaultValue - The default value (optional)
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          Component component,
                                          IModel<?> model,
                                          java.util.Locale locale,
                                          java.lang.String style,
                                          IModel<java.lang.String> defaultValue)
                                   throws java.util.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 for
        component - 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 locale
        style - If != null, it'll supersede the component's style
        defaultValue - The default value (optional)
        Returns:
        The string resource
        Throws:
        java.util.MissingResourceException - If resource not found and configuration dictates that exception should be thrown
      • getStringIgnoreSettings

        public java.lang.String getStringIgnoreSettings​(java.lang.String key,
                                                        Component component,
                                                        IModel<?> model,
                                                        java.lang.String defaultValue)
        Parameters:
        key - The key to obtain the resource for
        component - 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(String, Component, IModel, Locale, String, String)
      • getStringIgnoreSettings

        public java.lang.String getStringIgnoreSettings​(java.lang.String key,
                                                        Component component,
                                                        IModel<?> model,
                                                        java.util.Locale locale,
                                                        java.lang.String style,
                                                        java.lang.String defaultValue)
        This is similar to getString(String, Component, IModel, String) except that the resource settings are ignored. This allows to to code something like
         String 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 for
        component - 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 locale
        style - If != null, it'll supersede the component's style
        defaultValue - The default value (optional)
        Returns:
        The string resource
      • getStringResourceLoaders

        protected java.util.List<IStringResourceLoader> getStringResourceLoaders()
        In case you want to provide your own list of string resource loaders
        Returns:
        List of string resource loaders
      • putIntoCache

        protected void putIntoCache​(java.lang.String cacheKey,
                                    java.lang.String string)
        Put the value into the cache and associate it with the cache key
        Parameters:
        cacheKey -
        string -
      • getFromCache

        protected java.lang.String getFromCache​(java.lang.String cacheKey)
        Get the value associated with the key from the cache.
        Parameters:
        cacheKey -
        Returns:
        The value of the key
      • getCacheKey

        protected java.lang.String getCacheKey​(java.lang.String key,
                                               Component component,
                                               java.util.Locale locale,
                                               java.lang.String style,
                                               java.lang.String variation)
        Gets the cache key
        Parameters:
        key -
        component -
        locale - Guaranteed to be != null
        style -
        variation -
        Returns:
        The value of the key
      • substitutePropertyExpressions

        public java.lang.String substitutePropertyExpressions​(Component component,
                                                              java.lang.String string,
                                                              IModel<?> model)
        Helper method to handle property variable substitution in strings.
        Parameters:
        component - The component requesting a model value or {@code null]
        string - The string to substitute into
        model - The model
        Returns:
        The resulting string
      • setEnableCache

        public final void setEnableCache​(boolean value)
        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

        protected java.util.Map<java.lang.String,​java.lang.String> 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 the ConcurrentHashMap
        Returns:
        cache