Package org.jorigin.lang
Class LangResource
- java.lang.Object
-
- org.jorigin.lang.LangResource
-
public class LangResource extends java.lang.ObjectThis 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 voidaddResources(java.util.HashMap<java.lang.String,java.lang.String> resources)Add new map of lang resources to the current resources.voidaddResources(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.LocalegetLocale()Get the locale of the lang resource.java.lang.ObjectgetResource(java.lang.String key)Get a resource associated to a key.java.lang.ObjectgetResource(java.lang.String key, java.lang.String param)Get a resource associated to a key.java.lang.ObjectgetResource(java.lang.String key, java.lang.String[] params)Get a parametrized resource associated to a key.java.lang.StringgetResourcePath()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.StringgetString(java.lang.String key)Get a resource into a String form.java.lang.StringgetString(java.lang.String key, java.lang.String[] aParams)Get a parametrized resource into a String form.protected voidinit()Load all lang resource associated to the current locale.voidsetLocale(java.util.Locale locale)Set the locale of the lang resource.voidsetResourcePath(java.lang.String path)Set the root path of the resource language files.intsize()Get the number of resourced values.
-
-
-
Constructor Detail
-
LangResource
public LangResource()
Default constructor of lang resource. The default resource path is set to"ressource"+File.separator+"lang"+File.separatorand the default locale is set toSystem.getProperty("user.language")+"_"+System.getProperty("user.country").
If the system propertylang.resource.rootis 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 toSystem.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 propertylang.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 propertylang.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 toSystem.getProperty("user.language")+"_"+System.getProperty("user.country").- Parameters:
baseName- the path of the root of the localelocale- 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 returnparam- 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 returnparams- 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 ofgetResource(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 ofgetResource(key, aParams).toString();.- Parameters:
key- the key inditifying the resource to returnaParams- the parameters of the resource.- Returns:
- String he resource parametrized or the key if no resource is available.
-
-