Class LangResource

java.lang.Object
org.jorigin.lang.LangResource

public class LangResource extends 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.14" - b202111241200L
Author:
Julien SEINTURIER - Université de Toulon / CNRS LIS umr 7020 - github.com/jorigin/jcommon (contact@jorigin.org)
  • Constructor Details

    • 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

      public LangResource(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

      public LangResource(String baseName, 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 Details

    • init

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

      public void setLocale(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 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(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 String getResourcePath()
      Set the root path of the resource language files.
      Returns:
      the path of the resource language files root.
    • getResources

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

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

      public Set<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(HashMap<String,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 Object getResource(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 Object getResource(String key, 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 Object getResource(String key, 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 String getString(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 String getString(String key, 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.