Class LangResource


  • public class LangResource
    extends java.lang.Object
    This class enable to use lang localization in a Java application. The lang resource are stored in XML file and can be easily maintained without any link with the Java code.
    Since:
    1.0.0
    Version:
    "1.0.9" - b201903071030L
    Author:
    Julien Seinturier - COMEX S.A. - contact@jorigin.org - https://github.com/jorigin/jeometry
    • Constructor Summary

      Constructors 
      Constructor Description
      LangResource()
      Default constructor of lang resource.
      LangResource​(java.lang.String baseName)
      Construct a lang resource with the language file root given in parameter.
      LangResource​(java.lang.String baseName, java.util.Locale locale)
      Construct a lang resource with given path and for a given locale.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addResources​(java.util.HashMap<java.lang.String,​java.lang.String> resources)
      Add new map of lang resources to the current resources.
      void addResources​(LangResource resource)
      Add new lang resources to the current resources.
      java.util.ArrayList<java.lang.String> getAvailableLocales()
      Get the available locales.
      java.util.Set<java.lang.String> getKeySet()
      Get the keys attached to the values.
      java.util.Locale getLocale()
      Get the locale of the lang resource.
      java.lang.Object getResource​(java.lang.String key)
      Get a resource associated to a key.
      java.lang.Object getResource​(java.lang.String key, java.lang.String param)
      Get a resource associated to a key.
      java.lang.Object getResource​(java.lang.String key, java.lang.String[] params)
      Get a parametrized resource associated to a key.
      java.lang.String getResourcePath()
      Set the root path of the resource language files.
      java.util.HashMap<java.lang.String,​java.lang.String> getResources()
      Get the map containing the lang resources.
      java.lang.String getString​(java.lang.String key)
      Get a resource into a String form.
      java.lang.String getString​(java.lang.String key, java.lang.String[] aParams)
      Get a parametrized resource into a String form.
      protected void init()
      Load all lang resource associated to the current locale.
      void setLocale​(java.util.Locale locale)
      Set the locale of the lang resource.
      void setResourcePath​(java.lang.String path)
      Set the root path of the resource language files.
      int size()
      Get the number of resourced values.
      • Methods inherited from class java.lang.Object

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

      • LangResource

        public LangResource()
        Default constructor of lang resource. The default resource path is set to "ressource"+File.separator+"lang"+File.separator and the default locale is set to System.getProperty("user.language")+"_"+System.getProperty("user.country").
        If the system property lang.resource.root is set to a valid resource directory, it is then used instead of the default directory.
        The resources are loaded by the constructor.
        See Also:
        LangResource(String, Locale)
      • LangResource

        public LangResource​(java.lang.String baseName)
        Construct a lang resource with the language file root given in parameter. The locale is set by default to System.getProperty("user.language")+"_"+System.getProperty("user.country").
        If the given path is not valid, the resource will be loaded for the path given by the system property lang.resource.root.
        If the system property is not set or invalid, the default path is used as resource base.
        Parameters:
        baseName - the path of resource files directory
        See Also:
        LangResource(String, Locale)
      • LangResource

        public LangResource​(java.lang.String baseName,
                            java.util.Locale locale)
        Construct a lang resource with given path and for a given locale. The path is the root of the localized language files.
        If the given path is not valid, the resource will be loaded for the path given by the system property lang.resource.root.
        If the system property is not set or invalid, the path is set to "ressource"+File.separator+"lang"+File.separator. The locale string is made of [language]_[country]. By default the locale is set to System.getProperty("user.language")+"_"+System.getProperty("user.country").
        Parameters:
        baseName - the path of the root of the locale
        locale - the locale.
    • Method Detail

      • init

        protected void init()
        Load all lang resource associated to the current locale.
      • setLocale

        public void setLocale​(java.util.Locale locale)
        Set the locale of the lang resource. When a locale is set, all lang resource file associated to the locale are loaded from the resource path.
        Parameters:
        locale - the new locale.
      • getLocale

        public java.util.Locale getLocale()
        Get the locale of the lang resource. When a locale is set, all lang resource file associated to the locale are loaded from the resource path.
        Returns:
        the current locale.
      • setResourcePath

        public void setResourcePath​(java.lang.String path)
        Set the root path of the resource language files. The resources are reloaded from the new path.
        Parameters:
        path - the path of the resource language files root.
      • getResourcePath

        public java.lang.String getResourcePath()
        Set the root path of the resource language files.
        Returns:
        the path of the resource language files root.
      • getResources

        public java.util.HashMap<java.lang.String,​java.lang.String> getResources()
        Get the map containing the lang resources.
        Returns:
        a hash map containing the lang resource.
      • getAvailableLocales

        public java.util.ArrayList<java.lang.String> getAvailableLocales()
        Get the available locales.
        Returns:
        a list of available locales.
      • getKeySet

        public java.util.Set<java.lang.String> getKeySet()
        Get the keys attached to the values.
        Returns:
        the set of keys attached to the values.
      • size

        public int size()
        Get the number of resourced values.
        Returns:
        int the number of resourced values.
      • addResources

        public void addResources​(java.util.HashMap<java.lang.String,​java.lang.String> resources)
        Add new map of lang resources to the current resources.
        Parameters:
        resources - the new lang resources map to add.
      • addResources

        public void addResources​(LangResource resource)
        Add new lang resources to the current resources.
        Parameters:
        resource - the new lang resource to add.
      • getResource

        public java.lang.Object getResource​(java.lang.String key)
        Get a resource associated to a key. If the key does not correspond to any resource, the key itself is returned.
        Parameters:
        key - the key inditifying the resource to return
        Returns:
        Object the resource or the key if no resource is available.
      • getResource

        public java.lang.Object getResource​(java.lang.String key,
                                            java.lang.String param)
        Get a resource associated to a key. If the key does not correspond to any resource, the key itself is returned. The parameter given is applied to the resource if it accept parameter.
        Parameters:
        key - the key inditifying the resource to return
        param - the parameter to apply to the resource.
        Returns:
        Object the resource or the key if no resource is available.
      • getResource

        public java.lang.Object getResource​(java.lang.String key,
                                            java.lang.String[] params)
        Get a parametrized resource associated to a key. If the key does not correspond to any resource, the key itself is returned. Parameters enable to charaterize the resource. Parameter are identified in a resource by a % followed by the parameter number. For example, if the resource is "Hello %1 and %2.", and parameters are "Fox" and "Dana", the object returned is "Hello Fox and Dana."
        Parameters:
        key - the key identifying the resource to return
        params - the parameters of the resource.
        Returns:
        Object the resource parametrized or the key if no resource is available.
      • getString

        public java.lang.String getString​(java.lang.String key)
        Get a resource into a String form. This method is simply a call of getResource(key).toString();.
        Parameters:
        key - the key identifying the resource to return
        Returns:
        String the resource or the key if no resource is available.
      • getString

        public java.lang.String getString​(java.lang.String key,
                                          java.lang.String[] aParams)
        Get a parametrized resource into a String form. This method is simply a call of getResource(key, aParams).toString();.
        Parameters:
        key - the key inditifying the resource to return
        aParams - the parameters of the resource.
        Returns:
        String he resource parametrized or the key if no resource is available.