diff --git a/zanata-war/src/main/java/org/zanata/email/EmailBuilder.java b/zanata-war/src/main/java/org/zanata/email/EmailBuilder.java index 65fcb7ec7d..3482a53357 100644 --- a/zanata-war/src/main/java/org/zanata/email/EmailBuilder.java +++ b/zanata-war/src/main/java/org/zanata/email/EmailBuilder.java @@ -36,6 +36,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; +import org.zanata.i18n.MessagesFactory; import static com.googlecode.totallylazy.collections.PersistentMap.constructors.map; import static org.jboss.seam.ScopeType.EVENT; @@ -65,7 +66,7 @@ public EmailBuilder() { @In private Context emailContext; @In - private Messages msgs; + private MessagesFactory messagesFactory; private static VelocityEngine makeVelocityEngine() { VelocityEngine ve = new VelocityEngine(); @@ -148,9 +149,14 @@ MimeMessage buildMessage(MimeMessage msg, EmailStrategy strategy, InternetAddress[] toAddresses, String receivedReason) throws MessagingException { + // TODO remember users' locales, and customise for each recipient + // msgs = messagesFactory.getMessages(account.getLocale()); + Messages msgs = messagesFactory.getDefaultLocaleMessages(); + Optional from = strategy.getFromAddress(); + String fromName = msgs.get("jsf.Zanata"); msg.setFrom(from.or(Addresses.getAddress( - emailContext.getFromAddress(), emailContext.getFromName()))); + emailContext.getFromAddress(), fromName))); Optional replyTo = strategy.getReplyToAddress(); if (replyTo.isPresent()) { msg.setReplyTo(replyTo.get()); @@ -201,17 +207,12 @@ MimeMessage buildMessage(MimeMessage msg, EmailStrategy strategy, public static class Context { @In private ApplicationConfiguration applicationConfiguration; - @In - private Messages msgs; String getServerPath() { return applicationConfiguration.getServerPath(); } String getFromAddress() { return applicationConfiguration.getFromEmailAddr(); } - String getFromName() { - return msgs.get("jsf.Zanata"); - } } } diff --git a/zanata-war/src/main/java/org/zanata/i18n/Messages.java b/zanata-war/src/main/java/org/zanata/i18n/Messages.java index cecfdb7b13..1a199bfc53 100644 --- a/zanata-war/src/main/java/org/zanata/i18n/Messages.java +++ b/zanata-war/src/main/java/org/zanata/i18n/Messages.java @@ -1,5 +1,5 @@ /* - * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the + * Copyright 2010-2015, Red Hat, Inc. and individual contributors as indicated by the * @author tags. See the copyright.txt file in the distribution for a full * listing of individual contributors. * @@ -58,12 +58,24 @@ @Scope(EVENT) public class Messages extends AbstractMap { + /** + * Gets the 'messages' ResourceBundle for the locale of the current + * request, if any, otherwise server's default locale. + * @see org.jboss.seam.web.Locale + */ private static ResourceBundle getResourceBundle() { + return getResourceBundle(org.jboss.seam.core.Locale.instance()); + } + + /** + * Gets the 'messages' ResourceBundle for the specified locale. + */ + private static ResourceBundle getResourceBundle(java.util.Locale locale) { // Generic ResourceBundle without built-in interpolation: ResourceBundle resourceBundle = null; try { resourceBundle = ResourceBundle.getBundle( - "messages", org.jboss.seam.core.Locale.instance()); + "messages", locale); } catch (MissingResourceException e) { resourceBundle = new ResourceBundle() { @Override @@ -83,8 +95,23 @@ public Enumeration getKeys() { private final ResourceBundle resourceBundle; + /** + * Create an instance for the locale of the current request, if any, + * otherwise the server's default locale. + */ public Messages() { - this.resourceBundle = getResourceBundle(); + this(getResourceBundle()); + } + + /** + * Create an instance for the specified locale. + */ + public Messages(java.util.Locale locale) { + this(getResourceBundle(locale)); + } + + private Messages(ResourceBundle resourceBundle) { + this.resourceBundle = resourceBundle; } @Observer("org.jboss.seam.localeSelected") diff --git a/zanata-war/src/main/java/org/zanata/i18n/MessagesFactory.java b/zanata-war/src/main/java/org/zanata/i18n/MessagesFactory.java new file mode 100644 index 0000000000..c4f8f65915 --- /dev/null +++ b/zanata-war/src/main/java/org/zanata/i18n/MessagesFactory.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015, Red Hat, Inc. and individual contributors as indicated by the + * @author tags. See the copyright.txt file in the distribution for a full + * listing of individual contributors. + * + * This 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 2.1 of the License, or (at your option) + * any later version. + * + * This software 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 Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.zanata.i18n; + +import static org.jboss.seam.ScopeType.APPLICATION; + +import lombok.Getter; +import org.jboss.seam.annotations.AutoCreate; +import org.jboss.seam.annotations.Name; +import org.jboss.seam.annotations.Scope; + +import java.util.Locale; + +/** + * Factory bean to return an instance of Messages based on a parameter. + * + * @author Sean Flanigan sflaniga@redhat.com + */ +@AutoCreate +@Name("messagesFactory") +@Scope(APPLICATION) +public class MessagesFactory { + + /** + * Returns an instance of Messages for the server's default locale. + */ + @Getter + private final Messages defaultLocaleMessages = getMessages(Locale.getDefault()); + + /** + * Returns an instance of Messages for the specified locale. + */ + public Messages getMessages(Locale locale) { + return new Messages(locale); + } +} diff --git a/zanata-war/src/test/java/org/zanata/email/EmailStrategyTest.java b/zanata-war/src/test/java/org/zanata/email/EmailStrategyTest.java index a5d9146e96..7d4165d429 100644 --- a/zanata-war/src/test/java/org/zanata/email/EmailStrategyTest.java +++ b/zanata-war/src/test/java/org/zanata/email/EmailStrategyTest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Collection; +import java.util.Locale; import java.util.Properties; import javax.mail.BodyPart; @@ -42,6 +43,7 @@ import org.testng.annotations.Test; import org.zanata.common.ProjectType; import org.zanata.i18n.Messages; +import org.zanata.i18n.MessagesFactory; import org.zanata.webtrans.shared.model.ProjectIterationId; /** @@ -64,13 +66,20 @@ public String format(String key, Object... args) { } }; String fromAddress = "zanata@example.com"; - String fromName = "SERVER_NAME[测试]"; + String fromName = msgs.get("jsf.Zanata"); String toName = "User Name[测试]"; String toAddress = "username@example.com"; String serverPath = "https://zanata.example.com"; InternetAddress toAddr; InternetAddress[] toAddresses; + MessagesFactory msgsFactory = new MessagesFactory() { + @Override + public Messages getMessages(Locale locale) { + return msgs; + } + }; + Session session = Session.getDefaultInstance(new Properties()); EmailBuilder.Context context = new EmailBuilder.Context() { @Override @@ -82,13 +91,8 @@ String getFromAddress() { String getServerPath() { return serverPath; } - - @Override - String getFromName() { - return fromName; - } }; - EmailBuilder builder = new EmailBuilder(session, context, msgs); + EmailBuilder builder = new EmailBuilder(session, context, msgsFactory); MimeMessage message; // context values needed for some templates: