java.lang.Object
org.jorigin.lang.LangResource
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 Summary
ConstructorsConstructorDescriptionDefault constructor of lang resource.LangResource(String baseName) Construct a lang resource with the language file root given in parameter.LangResource(String baseName, Locale locale) Construct a lang resource with given path and for a given locale. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddResources(HashMap<String, String> resources) Add new map of lang resources to the current resources.voidaddResources(LangResource resource) Add new lang resources to the current resources.Get the available locales.Get the keys attached to the values.Get the locale of the lang resource.getResource(String key) Get a resource associated to a key.getResource(String key, String param) Get a resource associated to a key.getResource(String key, String[] params) Get a parametrized resource associated to a key.Set the root path of the resource language files.Get the map containing the lang resources.Get a resource into a String form.Get a parametrized resource into a String form.protected voidinit()Load all lang resource associated to the current locale.voidSet the locale of the lang resource.voidsetResourcePath(String path) Set the root path of the resource language files.intsize()Get the number of resourced values.
-
Constructor Details
-
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
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
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 Details
-
init
protected void init()Load all lang resource associated to the current locale. -
setLocale
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
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
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
Set the root path of the resource language files.- Returns:
- the path of the resource language files root.
-
getResources
Get the map containing the lang resources.- Returns:
- a hash map containing the lang resource.
-
getAvailableLocales
Get the available locales.- Returns:
- a list of available locales.
-
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
Add new map of lang resources to the current resources.- Parameters:
resources- the new lang resources map to add.
-
addResources
Add new lang resources to the current resources.- Parameters:
resource- the new lang resource to add.
-
getResource
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
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
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
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
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.
-