Class 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 :
    • 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
    • Constructor Detail

      • I18n

        public I18n()
    • Method Detail

      • 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
      • reload

        public static void reload()
      • setDefaultLocale

        public static void setDefaultLocale​(Locale locale)
        Sets the default locale used by I18n for the method t(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 t(String, Object...). Note : The I18n system must have been initialized by one of the init method.
        Returns:
        the default locale initialized in I18n
      • hasKey

        public static boolean hasKey​(Locale locale,
                                     String message)
        Look into the I18nLanguage associated to the locale if the given message can be found.
        Parameters:
        locale - the locale to be used to get the I18nLanguage
        message - 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 String l_​(Locale locale,
                                String message,
                                Object... args)
        Deprecated.
        since 2.9, will be removed soon or later, prefer use now method l(Locale, String, Object...)
        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 I18nMessageFormatter
        args - les parametres pour le message.
        Returns:
        la traduction si possible ou la chaine passee en parametre sinon.
        Since:
        2.1
      • 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 I18nMessageFormatter
        args - les parametres pour le message.
        Returns:
        la traduction si possible ou la chaine passee en parametre sinon.
        Since:
        2.1
      • t

        public static String t​(String message,
                               Object... args)
        Retourne la chaine traduite si possible.
        Parameters:
        message - message formate avec I18nMessageFormatter
        args - les parametres pour le message.
        Returns:
        la traduction si possible ou la chaine passee en parametre sinon.
      • n_

        @Deprecated
        public static String n_​(String message,
                                Object... args)
        Deprecated.
        since 2.9, will be removed soon or later, prefer use now method n(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 avec I18nMessageFormatter
        args - les parametres pour le message.
        Returns:
        le message passe en argument mais formatté avec les parametres
      • 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 I18nMessageFormatter
        args - 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

        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