branch develop updated (7f21d12 -> 4dac76e)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository i18n. See https://gitlab.nuiton.org/nuiton/i18n.git from 7f21d12 fixes #153: Update to struts2-core 2.5.16 new 4dac76e fixes #154: Add suport for struts text provider The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 4dac76edd8bc190db40e815c44eb2ec160dce09c Author: Eric Chatellier <chatellier@codelutin.com> Date: Fri Apr 20 11:43:40 2018 +0200 fixes #154: Add suport for struts text provider Summary of changes: nuiton-i18n/pom.xml | 6 + .../java/org/nuiton/i18n/web/I18nTextProvider.java | 183 +++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 nuiton-i18n/src/main/java/org/nuiton/i18n/web/I18nTextProvider.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository i18n. See https://gitlab.nuiton.org/nuiton/i18n.git commit 4dac76edd8bc190db40e815c44eb2ec160dce09c Author: Eric Chatellier <chatellier@codelutin.com> Date: Fri Apr 20 11:43:40 2018 +0200 fixes #154: Add suport for struts text provider --- nuiton-i18n/pom.xml | 6 + .../java/org/nuiton/i18n/web/I18nTextProvider.java | 183 +++++++++++++++++++++ 2 files changed, 189 insertions(+) diff --git a/nuiton-i18n/pom.xml b/nuiton-i18n/pom.xml index b1559aa..a1d472a 100644 --- a/nuiton-i18n/pom.xml +++ b/nuiton-i18n/pom.xml @@ -69,6 +69,12 @@ <artifactId>commons-io</artifactId> </dependency> + <dependency> + <groupId>org.apache.struts</groupId> + <artifactId>struts2-core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> diff --git a/nuiton-i18n/src/main/java/org/nuiton/i18n/web/I18nTextProvider.java b/nuiton-i18n/src/main/java/org/nuiton/i18n/web/I18nTextProvider.java new file mode 100644 index 0000000..9fe585f --- /dev/null +++ b/nuiton-i18n/src/main/java/org/nuiton/i18n/web/I18nTextProvider.java @@ -0,0 +1,183 @@ +/* + * #%L + * I18n :: Api + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2018 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.i18n.web; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.DefaultTextProvider; +import com.opensymphony.xwork2.TextProvider; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.commons.lang3.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. + * + * To use this text provider add the two following lines in your struts.xml : + * + * <pre> + * <bean class="org.nuiton.i18n.web.I18nTextProvider" name="i18nTextProvider" type="com.opensymphony.xwork2.TextProvider" /> + * <constant name="struts.textProvider" value="i18nTextProvider" /> + * </pre> + * + * Before struts 2.5, you may use "struts.xworkTextProvider" instead of {@link org.apache.struts2.StrutsConstants#STRUTS_TEXT_PROVIDER} + * + * @author Arnaud Thimel<thimel@codelutin.com> + * @since 3.7 + */ +public class I18nTextProvider implements TextProvider { + + private static final Log log = LogFactory.getLog(I18nTextProvider.class); + + public static final String UNTRANSLATED_MARKER = "???"; + + 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 boolean hasKey(String key) { + Locale locale = getLocale(); + boolean result = I18n.hasKey(locale, key); + return result; + } + + /** + * Implementation copied from {@link DefaultTextProvider#getText(String)} + */ + protected Locale getLocale() { + return ActionContext.getContext().getLocale(); + } + + protected String getTextFromLocale(String key, String defaultValue) { + Locale locale = 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 = 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; + } + +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm