r1855 - in trunk: nuiton-i18n/src/main/java/org/nuiton/i18n nuiton-i18n/src/test/java/org/nuiton/i18n src/site/apt
Author: tchemit Date: 2011-01-21 12:06:47 +0100 (Fri, 21 Jan 2011) New Revision: 1855 Url: http://nuiton.org/repositories/revision/i18n/1855 Log: begin of doc + let I18n init with default configuration instead of crashing it (but it might not translate anything...) Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java trunk/src/site/apt/application.apt trunk/src/site/apt/helloWorld.apt trunk/src/site/apt/presentation.apt 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-21 10:43:58 UTC (rev 1854) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-01-21 11:06:47 UTC (rev 1855) @@ -117,6 +117,46 @@ } /** + * Initialize I18n system. + * <p/> + * The {@code initializer} can be null, in that case it will use the default + * implementation of it given by the method {@link I18n#getDefaultInitializer()}. + * <p/> + * The {@code locale} can also be null, and in that case we will use the + * default locale of the system given by the method + * {@link Locale#getDefault()}. + * <p/> + * <strong>We strongly recommand not to use the default initializer</strong>, + * since this one scanq all the class-path to detects i18n files and can + * <strong>NOT</strong> garantee the order of loading such files. + * <p/> + * Prefer use at least the {@link DefaultI18nInitializer} instead. + * <p/> + * In version 3.0, we will try to make the {@link DefaultI18nInitializer} the + * default initializer using a convention over i18n files names. + * + * @param initializer the initializer to use to detect bundles + * @param locale the default locale to use + * @since 2.1 + */ + public static void init(I18nInitializer initializer, Locale locale) { + + if (initializer == null) { + + // get a default initializer + initializer = getDefaultInitializer(); + } + + if (locale == null) { + + // use default locale + locale = I18nUtil.newLocale(null, null); + } + + initStore(initializer, locale); + } + + /** * Initialize I18n with default initializer and default locale of the system. * <p/> * <strong>We recommand not to use any longer this method.</strong> @@ -129,7 +169,7 @@ if (log.isWarnEnabled()) { log.warn("Using deprecated init method."); } - init(getDefaultInitializer(), null); + init(null, (Locale) null); } /** @@ -147,7 +187,7 @@ if (log.isWarnEnabled()) { log.warn("Using deprecated init method."); } - init(getDefaultInitializer(), I18nUtil.newLocale(language, country)); + init(null, I18nUtil.newLocale(language, country)); } /** @@ -164,51 +204,11 @@ if (log.isWarnEnabled()) { log.warn("Using deprecated init method."); } - init(getDefaultInitializer(), locale); + init(null, locale); } - /** - * Initialize I18n system. - * <p/> - * The {@code initializer} can be null, in that case it will use the default - * implementation of it given by the method {@link I18n#getDefaultInitializer()}. - * <p/> - * The {@code locale} can also be null, and in that case we will use the - * default locale of the system given by the method - * {@link Locale#getDefault()}. - * <p/> - * <strong>We strongly recommand not to use the default initializer</strong>, - * since this one scanq all the class-path to detects i18n files and can - * <strong>NOT</strong> garantee the order of loading such files. - * <p/> - * Prefer use at least the {@link DefaultI18nInitializer} instead. - * <p/> - * In version 3.0, we will try to make the {@link DefaultI18nInitializer} the - * default initializer using a convention over i18n files names. - * - * @param initializer the initializer to use to detect bundles - * @param locale the default locale to use - * @since 2.1 - */ - public static void init(I18nInitializer initializer, Locale locale) { + public static void reload() { - if (initializer == null) { - - // get a default initializer - initializer = getDefaultInitializer(); - } - - if (locale == null) { - - // use default locale - locale = I18nUtil.newLocale(null, null); - } - - initStore(initializer, locale); - } - - public static void reload() throws IllegalStateException { - checkInit(); // get back initializer @@ -236,10 +236,9 @@ * {@code init} method. * * @param locale the new default locale. - * @throws IllegalStateException if I18n was not init * @since 2.1 */ - public static void setDefaultLocale(Locale locale) throws IllegalStateException { + public static void setDefaultLocale(Locale locale) { checkInit(); @@ -255,9 +254,8 @@ * {@code init} method. * * @return the default locale initialized in I18n - * @throws IllegalStateException if I18n was not init */ - public static Locale getDefaultLocale() throws IllegalStateException { + public static Locale getDefaultLocale() { checkInit(); @@ -438,9 +436,6 @@ */ @Deprecated public static I18nInitializer getInitializer() { -// if (initializer == null) { -// initializer = new ClassPathI18nInitializer(); -// } return initializer; } @@ -452,9 +447,6 @@ * @return the instanciated i18n store */ public static I18nStore getStore() { -// if (store == null) { -// store = new I18nStore(DEFAULT_LOCALE, getInitializer()); -// } return store; } @@ -528,6 +520,17 @@ return filter; } + /** + * Init the store with given parameters and set the current language in the + * store to the default given {@code locale}. + * <p/> + * All values must be none {@code null}. + * + * @param initializer the initializer to use to detect bundles + * @param locale the default locale to set in the store + * @throws NullPointerException if any parameter is {@code null} + */ + protected static void initStore(I18nInitializer initializer, Locale locale) throws NullPointerException { @@ -564,14 +567,26 @@ } /** - * Checks if the I18n was initialized. + * 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 {@link #init(I18nInitializer, Locale)} is + * mandatory if you want to be safe. * - * @throws IllegalStateException if I18n was not initialized * @since 2.1 */ - protected static void checkInit() throws IllegalStateException { + protected static void checkInit() { + if (store == null) { - throw new IllegalStateException("I18n was not initialized!"); + + if (log.isWarnEnabled()) { + + log.warn("\n\nI18n was not initialized! will init it with " + + "default initializer and default locale, it might " + + "not translate anything fro you...\nPlease use the " + + "method I18n.init(I18nInitializer, Locale) before " + + "any calling to a translation...\n\n"); + } + init(null, (Locale) null); } } Modified: trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java =================================================================== --- trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-21 10:43:58 UTC (rev 1854) +++ trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-01-21 11:06:47 UTC (rev 1855) @@ -54,34 +54,52 @@ I18n.close(); } - @Test(expected = IllegalStateException.class) + @Test public void testWithNoInit() { - I18n._("key.with.param"); + + String expected; + String actual; + + // pas de traduction possible car l'initialiser n'est pas bon :) + + expected = "key.with.param"; + actual = I18n._("key.with.param"); + Assert.assertEquals(expected, actual); + } - @Test(expected = IllegalStateException.class) + @Test public void testWithNoInit2() { - I18n.l_(Locale.FRANCE, "key.with.param"); + + String expected; + String actual; + + // pas de traduction possible car l'initialiser n'est pas bon :) + + expected = "key.with.param"; + actual = I18n.l_(Locale.FRANCE, "key.with.param"); + Assert.assertEquals(expected, actual); + + actual = I18n.l_(Locale.UK, "key.with.param"); + Assert.assertEquals(expected, actual); + } @Test public void testDefaultInit() { - String expected; - String actual; - Assert.assertNull(I18n.store); - I18n.init(); + I18n.init(null, (Locale) null); Assert.assertNotNull(I18n.store); Assert.assertNotNull(I18n.getDefaultLocale()); Assert.assertEquals(Locale.getDefault(), I18n.getDefaultLocale()); - Assert.assertNotNull(I18n.store.getLanguage()); - Assert.assertEquals(Locale.getDefault(), I18n.store.getLanguage().getLocale()); + Assert.assertNotNull(I18n.store.getCurrentLanguage()); + Assert.assertEquals(Locale.getDefault(), I18n.store.getCurrentLocale()); } Modified: trunk/src/site/apt/application.apt =================================================================== --- trunk/src/site/apt/application.apt 2011-01-21 10:43:58 UTC (rev 1854) +++ trunk/src/site/apt/application.apt 2011-01-21 11:06:47 UTC (rev 1855) @@ -57,19 +57,17 @@ package org.myOrganisation.myApplication; import org.myOrganisation.myLibrary.myLibrary; -import static org.nuiton.i18n.I18n._; -import static org.nuiton.i18n.I18n.init; -import static org.nuiton.i18n.I18n.setInitializer; +import org.nuiton.i18n.I18n; import org.nuiton.i18n.init.DefaultI18nInitializer; +import java.util.Locale; public class myApplication { public static void main(String[] args){ - setInitializer(new DefaultI18nInitializer("myApplication-i18n")); - init("fr","FR"); + I18n.init(new DefaultI18nInitializer("myApplication-i18n"), Locale.FRANCE); - System.out.println(_("String to translate in my application")); + System.out.println(I18n._("String to translate in my application")); myLibrary.myMethod(); } } Modified: trunk/src/site/apt/helloWorld.apt =================================================================== --- trunk/src/site/apt/helloWorld.apt 2011-01-21 10:43:58 UTC (rev 1854) +++ trunk/src/site/apt/helloWorld.apt 2011-01-21 11:06:47 UTC (rev 1855) @@ -71,7 +71,7 @@ la ligne ---- -org.nuiton.i18n.I18n.init("fr","FR"); +org.nuiton.i18n.I18n.init(Locale.FRANCE); ---- Modified: trunk/src/site/apt/presentation.apt =================================================================== --- trunk/src/site/apt/presentation.apt 2011-01-21 10:43:58 UTC (rev 1854) +++ trunk/src/site/apt/presentation.apt 2011-01-21 11:06:47 UTC (rev 1855) @@ -30,8 +30,8 @@ I18n.n_("Chaîne à ne pas traduire"). La librairie doit être initialisée au lancement de votre application par une - des méthodes I18n.init() qui peuvent prendre en paramètre, une locale, une - chaîne décrivant la langue,... + des méthodes I18n.init() qui peuvent prendre en paramètre, un initializer et + une locale, ou une chaîne décrivant la langue,... Les chaînes à traduire doivent être rentrées dans un fichier de propriétés situé dans le classpath de votre application, dans un dossier i18n, et nommé
participants (1)
-
tchemit@users.nuiton.org