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  {
20      private static final Serializable[] EMPTY_SERIALIZABLE = new Serializable[0];
21  
22      public String getText(String key, Serializable... arguments)
23      {
24          Serializable[] resolvedArguments = new Serializable[arguments.length];
25          for (int i = 0; i < arguments.length; i++)
26          {
27              Serializable argument = arguments[i];
28              if (argument instanceof Message)
29              {
30                  resolvedArguments[i] = getText((Message) argument);
31              }
32              else
33              {
34                  resolvedArguments[i] = arguments[i];
35              }
36          }
37          return resolveText(key, resolvedArguments);
38      }
39  
40      public String getText(Locale locale, String key, Serializable... arguments)
41      {
42          checkNotNull(locale);
43          Serializable[] resolvedArguments = new Serializable[arguments.length];
44          for (int i = 0; i < arguments.length; i++)
45          {
46              Serializable argument = arguments[i];
47              if (argument instanceof Message)
48              {
49                  resolvedArguments[i] = getText(locale, (Message) argument);
50              }
51              else
52              {
53                  resolvedArguments[i] = arguments[i];
54              }
55          }
56          return resolveText(locale, key, resolvedArguments);
57      }
58  
59      public String getText(String key)
60      {
61          return resolveText(key, EMPTY_SERIALIZABLE);
62      }
63  
64      public String getText(Locale locale, String key)
65      {
66          checkNotNull(locale);
67          return resolveText(locale, key, EMPTY_SERIALIZABLE);
68      }
69  
70      public String getText(Message message)
71      {
72          return getText(message.getKey(), message.getArguments());
73      }
74  
75      public String getText(Locale locale, Message message)
76      {
77          return getText(locale, message.getKey(), message.getArguments());
78      }
79  
80      /**
81       * Subclasses should implement this method to dispatch to a matching language in (in order of preference):
82       * <ul>
83       *     <li>the user's locale</li>
84       *     <li>the application's configured locale, or</li>
85       *     <li>the system default locale</li>
86       * </ul>
87       * @param key the key to translate.
88       * @param arguments the arguments to be inserted into the translated string.
89       * @return the translated string.
90       */
91      public abstract String resolveText(String key, Serializable[] arguments);
92  
93      /**
94       * Subclasses should implement this method to dispatch to a matching language in the given locale.
95       * @param locale the locale to translate into.
96       * @param key the key to translate.
97       * @param arguments the arguments to be inserted into the translated string.
98       * @return the translated string.
99       */
100     public abstract String resolveText(Locale locale, String key, Serializable[] arguments);
101 
102     public Message createMessage(String key, Serializable... arguments)
103     {
104         return new DefaultMessage(key, arguments);
105     }
106 
107     public MessageCollection createMessageCollection()
108     {
109         return new DefaultMessageCollection();
110     }
111 }