Author: fdesbois Date: 2011-05-06 14:49:50 +0200 (Fri, 06 May 2011) New Revision: 1904 Url: http://nuiton.org/repositories/revision/i18n/1904 Log: #1499 : Add I18nFormatter interface to format message with args. I18nFormat enum provides commons implementations such as STRING_FORMAT (default one in api) and MESSAGE_FORMAT. Added: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormat.java trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormatter.java Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_en_GB.properties trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_ja_JP.properties Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-05-04 19:30:55 UTC (rev 1903) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-05-06 12:49:50 UTC (rev 1904) @@ -73,6 +73,8 @@ /** Filtre a appliquer avant de retourner les chaines */ protected static I18nFilter filter; + protected static I18nFormatter formatter = I18nFormat.STRING_FORMAT; + /** * Change le filtre des chaines traduites * @@ -82,6 +84,10 @@ I18n.filter = filter; } + public static void setFormatter(I18nFormatter formatter) { + I18n.formatter = formatter; + } + /** * Initialize I18n system. * <p/> @@ -182,8 +188,7 @@ * Retourne la chaine traduite si possible dans la locale demandée. * * @param locale la locale dans lequel on souhaite la traduction - * @param message message formate avec la meme syntaxe que {@link - * String#format} + * @param message message formate avec {@link I18nFormatter} * @param args les parametres pour le message. * @return la traduction si possible ou la chaine passee en parametre * sinon. @@ -208,10 +213,10 @@ } try { - return applyFilter(String.format(result, args)); + return applyFilter(formatter.format(locale, result, args)); } catch (Exception eee) { try { - return applyFilter(String.format(message, args)); + return applyFilter(formatter.format(locale, message, args)); } catch (Exception zzz) { if (log.isWarnEnabled()) { log.warn( @@ -226,8 +231,7 @@ /** * Retourne la chaine traduite si possible. * - * @param message message formate avec la meme syntaxe que {@link - * String#format} + * @param message message formate avec {@link I18nFormatter} * @param args les parametres pour le message. * @return la traduction si possible ou la chaine passee en parametre * sinon. @@ -263,8 +267,7 @@ * devant être traduit seulement à leur lecture suivant la locale du lecteur * et non du créateur. * - * @param message message formate avec la meme syntaxe que {@link - * String#format(String, Object...)} + * @param message message formate avec {@link I18nFormatter} * @param args les parametres pour le message. * @return le message passe en argument mais formatté avec les parametres */ @@ -274,7 +277,10 @@ } try { - return String.format(message, args); + // XXX-fdesbois-2011-05-05 : don't know if it's relevant to format here, + // seems pretty useless because we will not use the value + // corresponding to given key message + return formatter.format(getDefaultLocale(), message, args); } catch (Exception eee) { if (log.isWarnEnabled()) { log.warn( @@ -379,6 +385,10 @@ return filter; } + public static I18nFormatter getFormatter() { + return formatter; + } + /** * Init the store with given parameters and set the current language in the * store to the default given {@code locale}. Added: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormat.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormat.java (rev 0) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormat.java 2011-05-06 12:49:50 UTC (rev 1904) @@ -0,0 +1,44 @@ +package org.nuiton.i18n; + +import java.text.MessageFormat; +import java.util.Formatter; +import java.util.Locale; + +/** + * Enum implementation for {@link I18nFormatter}. Provides basic formatter + * such as {@link #STRING_FORMAT} and {@link #MESSAGE_FORMAT}. + * <p/> + * Created: 05/05/11 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public enum I18nFormat implements I18nFormatter { + + /** + * Implementation based on printf syntax. + * + * @see Formatter + * @see String#format(Locale, String, Object...) + */ + STRING_FORMAT { + + @Override + public String format(Locale locale, String message, Object... args) { + return String.format(locale, message, args); + } + }, + + /** + * Implementation based on {@link MessageFormat} syntax. + */ + MESSAGE_FORMAT { + + @Override + public String format(Locale locale, String message, Object... args) { + MessageFormat formatter = new MessageFormat(message, locale); + return formatter.format(args); + } + }; + +} Property changes on: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormat.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormatter.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormatter.java (rev 0) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormatter.java 2011-05-06 12:49:50 UTC (rev 1904) @@ -0,0 +1,28 @@ +package org.nuiton.i18n; + +import java.util.Locale; + +/** + * Formatter to use at runtime when message need to be translated with args. + * <p/> + * Created: 05/05/11 + * + * @author fdesbois <desbois@codelutin.com> + * @see I18nFormat + * $Id$ + */ +public interface I18nFormatter { + + /** + * Format a given {@code message} with {@code locale} and {@code args}. + * The purpose is to replace placeholders in {@code message} with {@code args} + * value. This could depends on {@code locale} for date purpose for example. + * + * @param locale Locale to use for formatting (number, date, ...) + * @param message Message to format (that contains placeholders) + * @param args Object array to use in formatting the message + * @return the formatted message + */ + String format(Locale locale, String message, Object... args); + +} Property changes on: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nFormatter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java =================================================================== --- trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-05-04 19:30:55 UTC (rev 1903) +++ trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-05-06 12:49:50 UTC (rev 1904) @@ -30,6 +30,8 @@ import org.junit.Test; import org.nuiton.i18n.init.DefaultI18nInitializer; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.Locale; /** @@ -198,4 +200,28 @@ actual = I18n.l_(Locale.CHINA, "key.with.param", "param"); Assert.assertEquals(expected, actual); } + + @Test + public void testFormatterMessageFormat() { + + I18n.init(initializer, Locale.UK); + I18n.setFormatter(I18nFormat.MESSAGE_FORMAT); + + Date date = new GregorianCalendar(2011, 4, 5).getTime(); + + String expected = "Key with 05-May-2011"; + String actual = I18n.l_(Locale.UK, "key.with.date", date); + Assert.assertEquals(expected, actual); + + expected = "とキー 2011/05/05"; + actual = I18n.l_(Locale.JAPAN, "key.with.date", date); + Assert.assertEquals(expected, actual); + + expected = "Clé avec 5 mai 2011"; + actual = I18n.l_(Locale.FRANCE, "key.with.date", date); + Assert.assertEquals(expected, actual); + + // reset default value + I18n.setFormatter(I18nFormat.STRING_FORMAT); + } } Modified: trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_en_GB.properties =================================================================== --- trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_en_GB.properties 2011-05-04 19:30:55 UTC (rev 1903) +++ trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_en_GB.properties 2011-05-06 12:49:50 UTC (rev 1904) @@ -24,4 +24,5 @@ ### key.one=First key.two=Second -key.with.param=Key with %s \ No newline at end of file +key.with.param=Key with %s +key.with.date=Key with {0,date} \ No newline at end of file Modified: trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties =================================================================== --- trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties 2011-05-04 19:30:55 UTC (rev 1903) +++ trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties 2011-05-06 12:49:50 UTC (rev 1904) @@ -25,3 +25,4 @@ key.one=Premier key.two=Seconde key.with.param=Clé avec %s +key.with.date=Clé avec {0,date} Modified: trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_ja_JP.properties =================================================================== --- trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_ja_JP.properties 2011-05-04 19:30:55 UTC (rev 1903) +++ trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_ja_JP.properties 2011-05-06 12:49:50 UTC (rev 1904) @@ -25,3 +25,4 @@ key.one=最初の key.two=2番目 key.with.param=とキー %s +key.with.date=とキー {0,date}