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 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
67
68
69
70
71
72
73
74
75
76
77 public abstract String resolveText(String key, Serializable[] arguments);
78
79
80
81
82
83
84
85
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 }