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