r98 - in trunk: . nuiton-struts2 nuiton-struts2/src nuiton-struts2/src/main/java/org/nuiton/web/struts2 nuiton-struts2/src/test nuiton-struts2/src/test/java nuiton-struts2/src/test/java/org nuiton-struts2/src/test/java/org/nuiton nuiton-struts2/src/test/java/org/nuiton/web nuiton-struts2/src/test/java/org/nuiton/web/struts2 nuiton-struts2/src/test/resources nuiton-struts2/src/test/resources/META-INF
Author: athimel Date: 2011-08-03 10:27:27 +0200 (Wed, 03 Aug 2011) New Revision: 98 Url: http://nuiton.org/repositories/revision/nuiton-web/98 Log: #1651 : Create a TextProvider for Struts2 using nuiton's i18n Added: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/I18nTextProvider.java trunk/nuiton-struts2/src/test/ trunk/nuiton-struts2/src/test/java/ trunk/nuiton-struts2/src/test/java/org/ trunk/nuiton-struts2/src/test/java/org/nuiton/ trunk/nuiton-struts2/src/test/java/org/nuiton/web/ trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/ trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/I18nTextProviderTest.java trunk/nuiton-struts2/src/test/resources/ trunk/nuiton-struts2/src/test/resources/META-INF/ trunk/nuiton-struts2/src/test/resources/META-INF/toto-definition.properties trunk/nuiton-struts2/src/test/resources/META-INF/toto_fr_FR.properties Modified: trunk/nuiton-struts2/pom.xml trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/BaseAction.java trunk/pom.xml Modified: trunk/nuiton-struts2/pom.xml =================================================================== --- trunk/nuiton-struts2/pom.xml 2011-07-30 01:28:16 UTC (rev 97) +++ trunk/nuiton-struts2/pom.xml 2011-08-03 08:27:27 UTC (rev 98) @@ -31,6 +31,11 @@ </dependency> <dependency> + <groupId>org.nuiton.i18n</groupId> + <artifactId>nuiton-i18n</artifactId> + </dependency> + + <dependency> <groupId>org.nuiton.topia</groupId> <artifactId>topia-persistence</artifactId> </dependency> Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/BaseAction.java =================================================================== --- trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/BaseAction.java 2011-07-30 01:28:16 UTC (rev 97) +++ trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/BaseAction.java 2011-08-03 08:27:27 UTC (rev 98) @@ -25,13 +25,8 @@ package org.nuiton.web.struts2; import com.opensymphony.xwork2.ActionSupport; -import com.opensymphony.xwork2.util.ValueStack; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import java.util.Arrays; -import java.util.List; /** * Base action to use for a better usage of I18n. @@ -43,10 +38,6 @@ private static final long serialVersionUID = 1L; - public static final String UNTRANSLATED_MARKER = "???"; - - private static final Log log = LogFactory.getLog(BaseAction.class); - public String _(String aTextName) { String value = getText(aTextName); return value; @@ -57,80 +48,4 @@ return value; } - @Override - public String getText(String aTextName) { - String value = super.getText(aTextName); - return getSafeText(aTextName, value); - } - - @Override - public String getText(String aTextName, String defaultValue) { - String value = super.getText(aTextName, defaultValue); - return getSafeText(aTextName, value); - } - - @Override - public String getText(String aTextName, - String defaultValue, - String obj) { - String value = super.getText(aTextName, defaultValue, obj); - return getSafeText(aTextName, value); - } - - @Override - public String getText(String aTextName, List<?> args) { - String value = super.getText(aTextName, args); - return getSafeText(aTextName, value); - } - - @Override - public String getText(String key, String[] args) { - String value = super.getText(key, args); - return getSafeText(key, value); - } - - @Override - public String getText(String aTextName, - String defaultValue, - List<?> args) { - String value = super.getText(aTextName, defaultValue, args); - return getSafeText(aTextName, value); - } - - @Override - public String getText(String key, - String defaultValue, - String[] args) { - String value = super.getText(key, defaultValue, args); - return getSafeText(key, value); - } - - @Override - public String getText(String key, - String defaultValue, - List<?> args, - ValueStack stack) { - String value = super.getText(key, defaultValue, args, stack); - return getSafeText(key, value); - } - - @Override - public String getText(String key, - String defaultValue, - String[] args, - ValueStack stack) { - String value = super.getText(key, defaultValue, args, stack); - return getSafeText(key, value); - } - - protected String getSafeText(String key, String value) { - if (StringUtils.isEmpty(value)) { - if (log.isWarnEnabled()) { - log.warn("Key [" + key + "] is not translated"); - } - return UNTRANSLATED_MARKER + key + UNTRANSLATED_MARKER; - } - return value; - } - } Added: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/I18nTextProvider.java =================================================================== --- trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/I18nTextProvider.java (rev 0) +++ trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/I18nTextProvider.java 2011-08-03 08:27:27 UTC (rev 98) @@ -0,0 +1,183 @@ +/* + * #%L + * Nuiton Web :: Nuiton Struts 2 + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.web.struts2; + +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.ResourceBundleTextProvider; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.i18n.I18n; + +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +/** + * Struts2's TextProvider implementation using nuiton's i18n + * + * @author Arnaud Thimel<thimel@codelutin.com> + * @since 1.3 + */ +public class I18nTextProvider implements ResourceBundleTextProvider { + + private static final Log log = LogFactory.getLog(BaseAction.class); + + public static final String UNTRANSLATED_MARKER = "???"; + + protected LocaleProvider localeProvider; + + protected String getSafeText(String key, String value) { + if (StringUtils.isEmpty(value)) { + if (log.isWarnEnabled()) { + log.warn("Key [" + key + "] is not translated"); + } + return UNTRANSLATED_MARKER + key + UNTRANSLATED_MARKER; + } + return value; + } + + @Override + public void setBundle(ResourceBundle bundle) { + // Nothing to do + } + + @Override + public void setClazz(Class clazz) { + // Nothing to do + } + + @Override + public void setLocaleProvider(LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + + @Override + public boolean hasKey(String key) { + Locale locale = localeProvider.getLocale(); + boolean result = I18n.hasKey(locale, key); + return result; + } + + protected String getTextFromLocale(String key, String defaultValue) { + Locale locale = localeProvider.getLocale(); + String result = I18n.l_(locale, key); + if (result == null) { + result = defaultValue; + } + result = getSafeText(key, result); + return result; + } + + protected String getTextFromLocale(String key, String defaultValue, Object ... args) { + Locale locale = localeProvider.getLocale(); + String result = I18n.l_(locale, key, args); + if (result == null) { + result = defaultValue; + } + result = getSafeText(key, result); + return result; + } + + @Override + public String getText(String aTextName) { + String value = getTextFromLocale(aTextName, null); + return value; + } + + @Override + public String getText(String aTextName, String defaultValue) { + String value = getTextFromLocale(aTextName, defaultValue); + return value; + } + + @Override + public String getText(String aTextName, + String defaultValue, + String obj) { + String value = getTextFromLocale(aTextName, defaultValue, obj); + return value; + } + + @Override + public String getText(String aTextName, List<?> args) { + Object[] array = args.toArray(); + String value = getTextFromLocale(aTextName, null, array); + return value; + } + + @Override + public String getText(String key, String[] args) { + String value = getTextFromLocale(key, null, args); + return value; + } + + @Override + public String getText(String aTextName, + String defaultValue, + List<?> args) { + Object[] array = args.toArray(); + String value = getTextFromLocale(aTextName, defaultValue, array); + return value; + } + + @Override + public String getText(String key, + String defaultValue, + String[] args) { + String value = getTextFromLocale(key, defaultValue, args); + return value; + } + + @Override + public String getText(String key, + String defaultValue, + List<?> args, + ValueStack stack) { + String value = getText(key, defaultValue, args); + return value; + } + + @Override + public String getText(String key, + String defaultValue, + String[] args, + ValueStack stack) { + String value = getText(key, defaultValue, args); + return value; + } + + @Override + public ResourceBundle getTexts(String bundleName) { + return null; + } + + @Override + public ResourceBundle getTexts() { + return null; + } + +} Property changes on: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/I18nTextProvider.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/I18nTextProviderTest.java =================================================================== --- trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/I18nTextProviderTest.java (rev 0) +++ trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/I18nTextProviderTest.java 2011-08-03 08:27:27 UTC (rev 98) @@ -0,0 +1,95 @@ +/* + * #%L + * Nuiton Web :: Nuiton Struts 2 + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.web.struts2; + +import com.opensymphony.xwork2.LocaleProvider; +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.i18n.I18n; +import org.nuiton.i18n.init.DefaultI18nInitializer; + +import java.util.Arrays; +import java.util.Locale; + +public class I18nTextProviderTest { + + protected I18nTextProvider provider; + @After + public void after() throws Exception { + I18n.close(); + } + + @Before + public void init() throws Exception { + + DefaultI18nInitializer initializer = new DefaultI18nInitializer("toto"); + initializer.setMissingKeyReturnNull(true); + I18n.init(initializer, Locale.FRENCH); + + provider = new I18nTextProvider(); + provider.setLocaleProvider(new LocaleProvider() { + @Override + public Locale getLocale() { + return Locale.FRENCH; + } + }); + + } + + @Test + public void testHasKey() throws Exception { + Assert.assertFalse(provider.hasKey("missing")); + Assert.assertTrue(provider.hasKey("present")); + } + + @Test + public void testGetTextSimple() throws Exception { + Assert.assertEquals("yes", provider.getText("present")); + Assert.assertEquals(I18nTextProvider.UNTRANSLATED_MARKER + "missing" + I18nTextProvider.UNTRANSLATED_MARKER, provider.getText("missing")); + } + + @Test + public void testGetTextDefaultValue() throws Exception { + Assert.assertEquals("yes", provider.getText("present", "toto")); + Assert.assertEquals("toto", provider.getText("missing", "toto")); + } + + @Test + public void testGetTextArg() throws Exception { + Assert.assertEquals("Hello Arno !", provider.getText("hello", null, "Arno")); + } + + @Test + public void testGetTextArgs() throws Exception { + Assert.assertEquals("Hello Arno, ça biche ?", provider.getText("hello_plus", new String[] {"Arno", "ça biche"})); + Assert.assertEquals("Hello Moto, ça gaze ?", provider.getText("hello_plus", Arrays.asList("Moto", "ça gaze"))); + + Assert.assertEquals("Bonjour", provider.getText("hello_moins", "Bonjour", new String[] {"Arno", "ça biche"})); + Assert.assertEquals("Bonjour", provider.getText("hello_moins", "Bonjour", Arrays.asList("Moto", "ça gaze"))); + } + +} Property changes on: trunk/nuiton-struts2/src/test/java/org/nuiton/web/struts2/I18nTextProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-struts2/src/test/resources/META-INF/toto-definition.properties =================================================================== --- trunk/nuiton-struts2/src/test/resources/META-INF/toto-definition.properties (rev 0) +++ trunk/nuiton-struts2/src/test/resources/META-INF/toto-definition.properties 2011-08-03 08:27:27 UTC (rev 98) @@ -0,0 +1,28 @@ +### +# #%L +# Nuiton Web :: Nuiton Struts 2 +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2010 - 2011 CodeLutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/lgpl-3.0.html>. +# #L% +### + +bundles.fr_FR=bundleTest/toto_fr_FR.properties +encoding=utf-8 +locales=fr_FR Property changes on: trunk/nuiton-struts2/src/test/resources/META-INF/toto-definition.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-struts2/src/test/resources/META-INF/toto_fr_FR.properties =================================================================== --- trunk/nuiton-struts2/src/test/resources/META-INF/toto_fr_FR.properties (rev 0) +++ trunk/nuiton-struts2/src/test/resources/META-INF/toto_fr_FR.properties 2011-08-03 08:27:27 UTC (rev 98) @@ -0,0 +1,27 @@ +### +# #%L +# Nuiton Web :: Nuiton Struts 2 +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2010 - 2011 CodeLutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/lgpl-3.0.html>. +# #L% +### +present=yes +hello=Hello %s ! +hello_plus=Hello %s, %s ? \ No newline at end of file Property changes on: trunk/nuiton-struts2/src/test/resources/META-INF/toto_fr_FR.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-07-30 01:28:16 UTC (rev 97) +++ trunk/pom.xml 2011-08-03 08:27:27 UTC (rev 98) @@ -110,6 +110,14 @@ <!-- librairie topia --> <dependency> + <groupId>org.nuiton.i18n</groupId> + <artifactId>nuiton-i18n</artifactId> + <version>${nuitonI18nVersion}</version> + <scope>provided</scope> + </dependency> + + <!-- librairie topia --> + <dependency> <groupId>org.nuiton.topia</groupId> <artifactId>topia-persistence</artifactId> <version>${topiaVersion}</version> @@ -213,7 +221,9 @@ <!-- redmine configuration --> <projectId>nuiton-web</projectId> - <nuitonI18nPluginVersion>2.4</nuitonI18nPluginVersion> + <nuitonI18nVersion>2.4.1-SNAPSHOT</nuitonI18nVersion> + + <nuitonI18nPluginVersion>${nuitonI18nVersion}</nuitonI18nPluginVersion> <jredminePluginVersion>1.2</jredminePluginVersion> <topiaVersion>2.6.1-SNAPSHOT</topiaVersion>
participants (1)
-
athimel@users.nuiton.org