Package org.nuiton.i18n
Class I18n
java.lang.Object
org.nuiton.i18n.I18n
public class I18n
extends java.lang.Object
New generation I18n class.
Note: This class replace the previous one in project
nuiton-utils.
This class is a facility for internationalization. To use it in your soft,
you can either : - import the org.nuiton.i18n.I18n class,
- init the translation support with the init(String language) or init(String language, String country), init(Localelocale) static methods in your main, ( eg: I18n.init("fr","FR") )
- call the translate static method for each sentence, ( eg: I18n.l("hello you !") )
- create a resource file for each language following the naming convention given in the java.util.ResourceBundle javadoc and translate all the sentence.
- Since:
- 1.1
- Author:
- Tony Chemit - chemit@codelutin.com
-
Field Summary
Fields Modifier and Type Field Description protected static I18nFilterfilterFiltre a appliquer avant de retourner les chainesprotected static I18nInitializerinitializerDeprecated.since 2.1, will be removed in version 3.0, in stead use the methodinit(I18nInitializer, Locale)which will pass directly the initializer to the store.protected static I18nMessageFormattermessageFormatterFormatter to apply on each translation.protected static I18nStorestoreI18n store of languages -
Constructor Summary
Constructors Constructor Description I18n() -
Method Summary
Modifier and Type Method Description protected static java.lang.StringapplyFilter(java.lang.String message)Applique le filtre s'il y en a unprotected static voidcheckInit()Checks if the I18n was initialized and if not as a fall-back, init it with default initializer and default locale.static voidclose()Close i18n caches, says the store if exists.protected static I18nLanguagegetCurrentLanguage()Obtain the registred language from the store.protected static I18nInitializergetDefaultInitializer()static java.util.LocalegetDefaultLocale()Obtain the default locale setted in I18n.protected static I18nFiltergetFilter()protected static I18nLanguagegetLanguage(java.util.Locale locale)Obtain the language for the givenlocale.static I18nMessageFormattergetMessageFormatter()static I18nStoregetStore()Get the i18n store.static booleanhasKey(java.lang.String message)Look into the defaultI18nLanguageif the givenmessagecan be found.static booleanhasKey(java.util.Locale locale, java.lang.String message)static voidinit(I18nInitializer initializer, java.util.Locale locale)Initialize I18n system.protected static voidinitStore(I18nInitializer initializer, java.util.Locale locale)Init the store with given parameters and set the current language in the store to the default givenlocale.static java.lang.Stringl(java.util.Locale locale, java.lang.String message, java.lang.Object... args)Retourne la chaine traduite si possible dans la locale demandée.static java.lang.Stringl_(java.util.Locale locale, java.lang.String message, java.lang.Object... args)Deprecated.since 2.9, will be removed soon or later, prefer use now methodl(Locale, String, Object...)static java.lang.Stringn(java.lang.String message, java.lang.Object... args)Retourne la chaine passée en argument.static java.lang.Stringn_(java.lang.String message, java.lang.Object... args)Deprecated.since 2.9, will be removed soon or later, prefer use now methodn(String, Object...)static voidreload()static voidsetDefaultLocale(java.util.Locale locale)Sets the default locale used by I18n for the methodt(String, Object...).static voidsetFilter(I18nFilter filter)Change le filtre des chaines traduitesstatic java.lang.Stringt(java.lang.String message, java.lang.Object... args)Retourne la chaine traduite si possible.
-
Field Details
-
initializer
Deprecated.since 2.1, will be removed in version 3.0, in stead use the methodinit(I18nInitializer, Locale)which will pass directly the initializer to the store.I18n initializer, -
store
I18n store of languages -
filter
Filtre a appliquer avant de retourner les chaines -
messageFormatter
Formatter to apply on each translation. This formatter can not be configured directly here, but must be setted into theinitializerused to configure the I18n system.- Since:
- 2.4
- See Also:
I18nInitializer.setMessageFormatter(I18nMessageFormatter)
-
-
Constructor Details
-
I18n
public I18n()
-
-
Method Details
-
setFilter
Change le filtre des chaines traduites- Parameters:
filter- l'objet filtre a utiliser
-
init
Initialize I18n system. Theinitializercan be null, in that case it will use the default implementation of it given by the methodgetDefaultInitializer(). Thelocalecan also be null, and in that case we will use the default locale of the system given by the methodLocale.getDefault(). We strongly recommand not to use the default initializer, since this one scanq all the class-path to detects i18n files and can NOT garantee the order of loading such files. Prefer use at least theDefaultI18nInitializerinstead. In version 3.0, we will try to make theDefaultI18nInitializerthe default initializer using a convention over i18n files names.- Parameters:
initializer- the initializer to use to detect bundleslocale- the default locale to use- Since:
- 2.1
-
reload
public static void reload() -
setDefaultLocale
public static void setDefaultLocale(java.util.Locale locale)Sets the default locale used by I18n for the methodt(String, Object...). * As a side effect, it will also set this locale is the default locale in the system (says the methodLocale.getDefault()will return the given locale). Note : The I18n system must have been initialized by one of theinitmethod.- Parameters:
locale- the new default locale.- Since:
- 2.1
-
getDefaultLocale
public static java.util.Locale getDefaultLocale()Obtain the default locale setted in I18n. This very locale is used in translation when no locale information is given (says in methodt(String, Object...). Note : The I18n system must have been initialized by one of theinitmethod.- Returns:
- the default locale initialized in I18n
-
hasKey
public static boolean hasKey(java.lang.String message)Look into the defaultI18nLanguageif the givenmessagecan be found.- Parameters:
message- the message to check presence- Returns:
- true/false whether the message is present in the default language
- Since:
- 2.4.1
- See Also:
getDefaultLocale(),hasKey(java.util.Locale, String)
-
hasKey
public static boolean hasKey(java.util.Locale locale, java.lang.String message)- Parameters:
locale- the locale to be used to get the I18nLanguagemessage- the message to check presence- Returns:
- true/false whether the message is present for the given locale
- Since:
- 2.4.1
-
l_
@Deprecated public static java.lang.String l_(java.util.Locale locale, java.lang.String message, java.lang.Object... args)Deprecated.since 2.9, will be removed soon or later, prefer use now methodl(Locale, String, Object...)Retourne la chaine traduite si possible dans la locale demandée.- Parameters:
locale- la locale dans lequel on souhaite la traductionmessage- message formate avecI18nMessageFormatterargs- les parametres pour le message.- Returns:
- la traduction si possible ou la chaine passee en parametre sinon.
- Since:
- 2.1
-
l
public static java.lang.String l(java.util.Locale locale, java.lang.String message, java.lang.Object... args)Retourne la chaine traduite si possible dans la locale demandée.- Parameters:
locale- la locale dans lequel on souhaite la traductionmessage- message formate avecI18nMessageFormatterargs- les parametres pour le message.- Returns:
- la traduction si possible ou la chaine passee en parametre sinon.
- Since:
- 2.1
-
t
public static java.lang.String t(java.lang.String message, java.lang.Object... args)Retourne la chaine traduite si possible.- Parameters:
message- message formate avecI18nMessageFormatterargs- les parametres pour le message.- Returns:
- la traduction si possible ou la chaine passee en parametre sinon.
-
n_
@Deprecated public static java.lang.String n_(java.lang.String message, java.lang.Object... args)Deprecated.since 2.9, will be removed soon or later, prefer use now methodn(String, Object...)Retourne la chaine passée en argument. Utile surtout pour collecter les chaines et ne pas les traduires à leur apparition. Par exemple :String key = "nuitonutils.key"; String result = l(key)
fonctionnera, mais la chaine n'aura pas été marquée comme devant être internationalisé. Tres utile par exemple, pour crée des objets non internationnalisé, et devant être traduit seulement à leur lecture suivant la locale du lecteur et non du créateur.- Parameters:
message- message formate avecI18nMessageFormatterargs- les parametres pour le message.- Returns:
- le message passe en argument mais formatté avec les parametres
-
n
public static java.lang.String n(java.lang.String message, java.lang.Object... args)Retourne la chaine passée en argument. Utile surtout pour collecter les chaines et ne pas les traduires à leur apparition. Par exemple :String key = "nuitonutils.key"; String result = l(key)
fonctionnera, mais la chaine n'aura pas été marquée comme devant être internationalisé. Tres utile par exemple, pour crée des objets non internationnalisé, et devant être traduit seulement à leur lecture suivant la locale du lecteur et non du créateur.- Parameters:
message- message formate avecI18nMessageFormatterargs- les parametres pour le message.- Returns:
- le message passe en argument mais formatté avec les parametres
-
close
public static void close()Close i18n caches, says the store if exists. This method should be called to reset all caches (languages, bundles,...) -
getStore
Get the i18n store. If store is not init, then instanciate it.- Returns:
- the instanciated i18n store
-
applyFilter
protected static java.lang.String applyFilter(java.lang.String message)Applique le filtre s'il y en a un- Parameters:
message- le message qui devrait etre retourne avant application du filtre.- Returns:
- le message filtre
-
getCurrentLanguage
Obtain the registred language from the store. If no language were registred in the store, then use the language with the default locale of the store.- Returns:
- the current language of the store, or the default one if store is not init.
-
getLanguage
Obtain the language for the givenlocale. If locale isnull, it means we wants to use the language registred in the store. As a fallback if this language is not defined, we use the language of the default locale of the store.- Parameters:
locale- the required locale ornullif we wants to use the one from the store- Returns:
- the language associated with the given locale.
- Since:
- 2.1
-
getFilter
-
getMessageFormatter
-
initStore
protected static void initStore(I18nInitializer initializer, java.util.Locale locale) throws java.lang.NullPointerExceptionInit the store with given parameters and set the current language in the store to the default givenlocale. All values must be nonenull.- Parameters:
initializer- the initializer to use to detect bundleslocale- the default locale to set in the store- Throws:
java.lang.NullPointerException- if any parameter isnull
-
getDefaultInitializer
-
checkInit
protected static void checkInit()Checks if the I18n was initialized and if not as a fall-back, init it with default initializer and default locale. It could not works for you... A call to the methodinit(I18nInitializer, Locale)is mandatory if you want to be safe.- Since:
- 2.1
-