View Javadoc

1   package com.atlassian.sal.core.message;
2   
3   import com.atlassian.sal.api.message.I18nResolver;
4   import com.atlassian.sal.api.message.Message;
5   import com.atlassian.sal.api.message.MessageCollection;
6   
7   import java.io.Serializable;
8   import java.util.Locale;
9   
10  import static com.google.common.base.Preconditions.checkNotNull;
11  
12  /**
13   * Basic skeleton of an implementation of {@link I18nResolver}, which host applications can use to get started.
14   * Subclasses should only need to implement the abstract methods {@link #resolveText(String, java.io.Serializable[])},
15   * {@link #resolveText(Locale, String, Serializable[])}, {@link #getRawText(String)} and
16   * {@link #getRawText(java.util.Locale, String)}.
17   */
18  public abstract class AbstractI18nResolver implements I18nResolver {
19      private static final Serializable[] EMPTY_SERIALIZABLE = new Serializable[0];
20  
21      public String getText(String key, Serializable... arguments) {
22          Serializable[] resolvedArguments = new Serializable[arguments.length];
23          for (int i = 0; i < arguments.length; i++) {
24              Serializable argument = arguments[i];
25              if (argument instanceof Message) {
26                  resolvedArguments[i] = getText((Message) argument);
27              } else {
28                  resolvedArguments[i] = arguments[i];
29              }
30          }
31          return resolveText(key, resolvedArguments);
32      }
33  
34      public String getText(Locale locale, String key, Serializable... arguments) {
35          checkNotNull(locale);
36          Serializable[] resolvedArguments = new Serializable[arguments.length];
37          for (int i = 0; i < arguments.length; i++) {
38              Serializable argument = arguments[i];
39              if (argument instanceof Message) {
40                  resolvedArguments[i] = getText(locale, (Message) argument);
41              } else {
42                  resolvedArguments[i] = arguments[i];
43              }
44          }
45          return resolveText(locale, key, resolvedArguments);
46      }
47  
48      public String getText(String key) {
49          return resolveText(key, EMPTY_SERIALIZABLE);
50      }
51  
52      public String getText(Locale locale, String key) {
53          checkNotNull(locale);
54          return resolveText(locale, key, EMPTY_SERIALIZABLE);
55      }
56  
57      public String getText(Message message) {
58          return getText(message.getKey(), message.getArguments());
59      }
60  
61      public String getText(Locale locale, Message message) {
62          return getText(locale, message.getKey(), message.getArguments());
63      }
64  
65      /**
66       * Subclasses should implement this method to dispatch to a matching language in (in order of preference):
67       * <ul>
68       * <li>the user's locale</li>
69       * <li>the application's configured locale, or</li>
70       * <li>the system default locale</li>
71       * </ul>
72       *
73       * @param key       the key to translate.
74       * @param arguments the arguments to be inserted into the translated string.
75       * @return the translated string.
76       */
77      public abstract String resolveText(String key, Serializable[] arguments);
78  
79      /**
80       * Subclasses should implement this method to dispatch to a matching language in the given locale.
81       *
82       * @param locale    the locale to translate into.
83       * @param key       the key to translate.
84       * @param arguments the arguments to be inserted into the translated string.
85       * @return the translated string.
86       */
87      public abstract String resolveText(Locale locale, String key, Serializable[] arguments);
88  
89      public Message createMessage(String key, Serializable... arguments) {
90          return new DefaultMessage(key, arguments);
91      }
92  
93      public MessageCollection createMessageCollection() {
94          return new DefaultMessageCollection();
95      }
96  }