Author: tchemit Date: 2011-01-20 14:55:29 +0100 (Thu, 20 Jan 2011) New Revision: 1850 Url: http://nuiton.org/repositories/revision/i18n/1850 Log: make finally works I18n with all the lazy stuff... Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-01-20 10:56:19 UTC (rev 1849) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-01-20 13:55:29 UTC (rev 1850) @@ -74,6 +74,8 @@ /** Filtre a appliquer avant de retourner les chaines */ protected static I18nFilter filter; + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + /** * Sets the initializer to use to init the I18n system. * <p/> @@ -119,6 +121,7 @@ */ public static void init(Locale locale) { if (locale == null) { + // use default locale locale = I18nUtil.newLocale(null, null); } @@ -130,7 +133,7 @@ /** * Retourne la chaine traduite si possible dans la locale demandée. * - * @param locale la locale dans lequel on souhaite la traduction + * @param locale la locale dans lequel on souhaite la traduction * @param message la chaine a traduire * @return la traduction si possible ou la chaine passee en parametre * sinon. @@ -143,7 +146,7 @@ return null; } - I18nLanguage language = getStore().getLanguage(locale); + I18nLanguage language = getLanguage(locale); String result = message; if (language != null) { result = language.translate(message); @@ -169,7 +172,7 @@ return null; } - I18nLanguage language = getStore().getLanguage(locale); + I18nLanguage language = getLanguage(locale); String result = message; if (language != null) { result = language.translate(message); @@ -198,7 +201,16 @@ * sinon. */ public static String _(String message) { + + // if the key to translate is null, just return null + if (message == null) { + return null; + } + + // get current locale (from the language in the store) Locale locale = getCurrentLanguage().getLocale(); + + // translate with this locale String result = l_(locale, message); return result; } @@ -213,7 +225,16 @@ * sinon. */ public static String _(String message, Object... args) { + + // if the key to translate is null, just return null + if (message == null) { + return null; + } + + // get current locale (from the language in the store) Locale locale = getCurrentLanguage().getLocale(); + + // translate with this locale String result = l_(locale, message, args); return result; } @@ -288,11 +309,17 @@ } } + /** + * Get the i18n initializer. + * <p/> + * If no one was specified, then use the {@link ClassPathI18nInitializer}. + * + * @return the i81n initializer to used to init system + */ public static I18nInitializer getInitializer() { if (initializer == null) { initializer = new ClassPathI18nInitializer(); } - return initializer; } @@ -304,7 +331,6 @@ * @return the instanciated i18n store */ public static I18nStore getStore() { - if (store == null) { store = new I18nStore(DEFAULT_LOCALE, getInitializer()); } @@ -326,14 +352,60 @@ } /** - * @return the current language of the store, or {@code null} if store is + * Obtain the registred language from the store. + * <p/> + * If no language were registred in the store, then use the language + * with the default locale of the store. + * + * @return the current language of the store, or the default one if store is * not init. */ protected static I18nLanguage getCurrentLanguage() { - I18nLanguage language = getStore().getLanguage(); +// I18nLanguage language = getStore().getLanguage(); + I18nLanguage language = getLanguage(null); return language; } + /** + * Obtain the language for the given {@code locale}. + * <p/> + * If locale is {@code null}, it means we wants to use the language + * registred in the store. + * <p/> + * As a fallback if this language is not defined, we use the language of + * the default locale of the store. + * + * @param locale the required locale or {@code null} if we wants to use the + * one from the store + * @return the language associated with the given locale. + * @since 2.1 + */ + protected static I18nLanguage getLanguage(Locale locale) { + I18nLanguage language; + if (locale == null) { + + // default locale required : means wants the one of the store + + language = getStore().getLanguage(); + + if (language == null) { + + // store was not initialized + + // use the default locale of the store + locale = getStore().getDefaultLocale(); + + // get the language for this default locale + language = getStore().getLanguage(locale); + } + } else { + + // get the given language from the store + language = getStore().getLanguage(locale); + } + return language; + } + protected static I18nFilter getFilter() { return filter; }
participants (1)
-
tchemit@users.nuiton.org