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