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
14
15
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
81
82
83
84
85
86
87
88
89
90 public abstract String resolveText(String key, Serializable[] arguments);
91
92
93
94
95
96
97
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 }