org.nuiton.i18n
Class I18n

java.lang.Object
  extended by org.nuiton.i18n.I18n

public class I18n
extends 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 :

Since:
1.1
Author:
tchemit

Field Summary
static String DEFAULT_ENCODING
          Deprecated. since 2.1, will be removed in version 3.0
static Locale DEFAULT_LOCALE
          Deprecated. since 2.1, will be removed in version 3.0
protected static I18nFilter filter
          Filtre a appliquer avant de retourner les chaines
protected static I18nInitializer initializer
          Deprecated. since 2.1, will be removed in version 3.0, in stead use the method init(I18nInitializer, Locale) which will pass directly the initializer to the store.
static String ISO_8859_1_ENCONDING
          Deprecated. since 2.1, will be removed in version 3.0
protected static I18nStore store
          I18n store of languages
static String UTF_8_ENCONDING
          Deprecated. since 2.1, will be removed in version 3.0
 
Constructor Summary
I18n()
           
 
Method Summary
static String _(String message)
          Deprecated. since 2.1, will be removed in version 3.0, use instead the method _(String, Object...).
static String _(String message, Object... args)
          Retourne la chaine traduite si possible.
protected static String applyFilter(String message)
          Applique le filtre s'il y en a un
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.
static void close()
          Close i18n caches, says the store if exists.
protected static I18nLanguage getCurrentLanguage()
          Obtain the registred language from the store.
protected static I18nInitializer getDefaultInitializer()
           
static Locale getDefaultLocale()
          Obtain the default locale setted in I18n.
protected static I18nFilter getFilter()
           
static I18nInitializer getInitializer()
          Deprecated. since 2.1, will be removed in version 3.0, the initializer will no more be saved in this class but directly in the I18nStore.
protected static I18nLanguage getLanguage(Locale locale)
          Obtain the language for the given locale.
static I18nStore getStore()
          Get the i18n store.
static void init()
          Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.
static void init(I18nInitializer initializer, Locale locale)
          Initialize I18n system.
static void init(Locale locale)
          Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.
static void init(String language, String country)
          Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.
protected static void initStore(I18nInitializer initializer, Locale locale)
          Init the store with given parameters and set the current language in the store to the default given locale.
static String l_(Locale locale, String message, Object... args)
          Retourne la chaine traduite si possible dans la locale demandée.
static String n_(String message)
          Deprecated. since 2.1, will be removed in version 3.0, use instead the method n_(String, Object...).
static String n_(String message, Object... args)
          Retourne la chaine passée en argument.
static void reload()
           
static void setDefaultLocale(Locale locale)
          Sets the default locale used by I18n for the method _(String, Object...).
static void setFilter(I18nFilter filter)
          Change le filtre des chaines traduites
static void setInitializer(I18nInitializer initializer)
          Deprecated. since 2.1, will be removed in version 3.0, use instead the method init(I18nInitializer, Locale).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ISO_8859_1_ENCONDING

@Deprecated
public static final String ISO_8859_1_ENCONDING
Deprecated. since 2.1, will be removed in version 3.0
See Also:
Constant Field Values

UTF_8_ENCONDING

@Deprecated
public static final String UTF_8_ENCONDING
Deprecated. since 2.1, will be removed in version 3.0
See Also:
Constant Field Values

DEFAULT_ENCODING

@Deprecated
public static final String DEFAULT_ENCODING
Deprecated. since 2.1, will be removed in version 3.0
See Also:
Constant Field Values

DEFAULT_LOCALE

@Deprecated
public static final Locale DEFAULT_LOCALE
Deprecated. since 2.1, will be removed in version 3.0

initializer

@Deprecated
protected static I18nInitializer initializer
Deprecated. since 2.1, will be removed in version 3.0, in stead use the method init(I18nInitializer, Locale) which will pass directly the initializer to the store.
I18n initializer,


store

protected static I18nStore store
I18n store of languages


filter

protected static I18nFilter filter
Filtre a appliquer avant de retourner les chaines

Constructor Detail

I18n

public I18n()
Method Detail

setInitializer

@Deprecated
public static void setInitializer(I18nInitializer initializer)
Deprecated. since 2.1, will be removed in version 3.0, use instead the method init(I18nInitializer, Locale).

Sets the initializer to use to init the I18n system.

You must inovke this method before any invocation of methods init(XXX).

Parameters:
initializer - the initializer to set.
See Also:
DefaultI18nInitializer

setFilter

public static void setFilter(I18nFilter filter)
Change le filtre des chaines traduites

Parameters:
filter - l'objet filtre a utiliser

init

public static void init(I18nInitializer initializer,
                        Locale locale)
Initialize I18n system.

The initializer can be null, in that case it will use the default implementation of it given by the method getDefaultInitializer().

The locale can also be null, and in that case we will use the default locale of the system given by the method Locale.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 the DefaultI18nInitializer instead.

In version 3.0, we will try to make the DefaultI18nInitializer the default initializer using a convention over i18n files names.

Parameters:
initializer - the initializer to use to detect bundles
locale - the default locale to use
Since:
2.1

init

@Deprecated
public static void init()
Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.

Initialize I18n with default initializer and default locale of the system.

We recommand not to use any longer this method.


init

@Deprecated
public static void init(String language,
                                   String country)
Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.

Initialize I18n with default initializer and given locale.

We recommand not to use any longer this method.

Parameters:
language - une chaine representant la langue à utiliser fr, en, ...
country - une chaine representant le pays à utiliser FR, GB, ...

init

@Deprecated
public static void init(Locale locale)
Deprecated. since 2.1, will be removed in version 3.0, prefer use the init(I18nInitializer, Locale) instead.

Initialize I18n with default initializer and given locale.

We recommand not to use any longer this method.

Parameters:
locale - language to use

reload

public static void reload()

setDefaultLocale

public static void setDefaultLocale(Locale locale)
Sets the default locale used by I18n for the method _(String, Object...). *

As a side effect, it will also set this locale is the default locale in the system (says the method Locale.getDefault() will return the given locale).

Note : The I18n system must have been initialized by one of the init method.

Parameters:
locale - the new default locale.
Since:
2.1

getDefaultLocale

public static Locale getDefaultLocale()
Obtain the default locale setted in I18n. This very locale is used in translation when no locale information is given (says in method _(String, Object...).

Note : The I18n system must have been initialized by one of the init method.

Returns:
the default locale initialized in I18n

l_

public static String l_(Locale locale,
                        String message,
                        Object... args)
Retourne la chaine traduite si possible dans la locale demandée.

Parameters:
locale - la locale dans lequel on souhaite la traduction
message - message formate avec la meme syntaxe que String.format(java.lang.String, java.lang.Object...)
args - les parametres pour le message.
Returns:
la traduction si possible ou la chaine passee en parametre sinon.
Since:
2.1

_

@Deprecated
public static String _(String message)
Deprecated. since 2.1, will be removed in version 3.0, use instead the method _(String, Object...).

Retourne la chaine traduite si possible.

Parameters:
message - la chaine a traduire
Returns:
la traduction si possible ou la chaine passee en parametre sinon.

_

public static String _(String message,
                       Object... args)
Retourne la chaine traduite si possible.

Parameters:
message - message formate avec la meme syntaxe que String.format(java.lang.String, java.lang.Object...)
args - les parametres pour le message.
Returns:
la traduction si possible ou la chaine passee en parametre sinon.

n_

public static String n_(String message,
                        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 avec la meme syntaxe que String.format(String, Object...)
args - les parametres pour le message.
Returns:
le message passe en argument mais formatté avec les parametres

n_

@Deprecated
public static String n_(String message)
Deprecated. since 2.1, will be removed in version 3.0, use instead the method n_(String, Object...).

Retourne la chaine passé 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 - la chaine à traduire
Returns:
la chaine passée en argument.

close

public static void close()
Close i18n caches, says the store if exists.

This method should be called to reset all caches (languages, bundles,...)


getInitializer

@Deprecated
public static I18nInitializer getInitializer()
Deprecated. since 2.1, will be removed in version 3.0, the initializer will no more be saved in this class but directly in the I18nStore.

Get the i18n initializer.

If no one was specified, then use the ClassPathI18nInitializer.

Returns:
the i81n initializer to used to init system

getStore

public static I18nStore getStore()
Get the i18n store.

If store is not init, then instanciate it.

Returns:
the instanciated i18n store

applyFilter

protected static String applyFilter(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

protected static I18nLanguage 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

protected static I18nLanguage getLanguage(Locale locale)
Obtain the language for the given locale.

If locale is null, 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 or null if we wants to use the one from the store
Returns:
the language associated with the given locale.
Since:
2.1

getFilter

protected static I18nFilter getFilter()

initStore

protected static void initStore(I18nInitializer initializer,
                                Locale locale)
                         throws NullPointerException
Init the store with given parameters and set the current language in the store to the default given locale.

All values must be none null.

Parameters:
initializer - the initializer to use to detect bundles
locale - the default locale to set in the store
Throws:
NullPointerException - if any parameter is null

getDefaultInitializer

protected static I18nInitializer 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 method init(I18nInitializer, Locale) is mandatory if you want to be safe.

Since:
2.1


Copyright © 2004-2011 CodeLutin. All Rights Reserved.