1 |
package skrueger.i8n; |
2 |
|
3 |
import java.util.Locale; |
4 |
import java.util.Set; |
5 |
import java.util.TreeSet; |
6 |
|
7 |
import org.apache.log4j.Logger; |
8 |
|
9 |
public class I8NUtil { |
10 |
static final Logger LOGGER = Logger.getLogger(I8NUtil.class); |
11 |
|
12 |
private static Set<String> languageCodes = new TreeSet<String>(); |
13 |
static { |
14 |
for (Locale locale : java.util.Locale.getAvailableLocales()) { |
15 |
getLanguageCodes().add(locale.getLanguage()); |
16 |
} |
17 |
// for (String code : java.util.Locale.getISOLanguages()) { |
18 |
// getLanguageCodes().add(code); |
19 |
// } |
20 |
} |
21 |
|
22 |
/** |
23 |
* Creates a {@link Translation}<br/> |
24 |
* <p> |
25 |
* <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set. |
26 |
* <li>If format can't be recognized, the {@link String} is interpreted as |
27 |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
28 |
* |
29 |
* @author Stefan Alfons Krüger |
30 |
*/ |
31 |
public static Translation createFromOneLIne(final String oneLineCoded) { |
32 |
Translation result = new Translation(); |
33 |
result.fromOneLine(oneLineCoded); |
34 |
return result; |
35 |
} |
36 |
|
37 |
/** |
38 |
* Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/> |
39 |
* |
40 |
* |
41 |
* @author Stefan Alfons Krüger |
42 |
*/ |
43 |
public static String toOneLine(Translation source) { |
44 |
StringBuffer oneLine = new StringBuffer(); |
45 |
for (String key : source.keySet()) { |
46 |
oneLine.append(key + "{" + source.get(key) + "}"); |
47 |
} |
48 |
return oneLine.toString(); |
49 |
} |
50 |
|
51 |
/** |
52 |
* @author Stefan Alfons Krüger |
53 |
* @param code |
54 |
* @return true if the code paramter is a valid ISO Language code |
55 |
*/ |
56 |
public static boolean isValidISOLangCode(String code) { |
57 |
return getLanguageCodes().contains(code); |
58 |
} |
59 |
|
60 |
/** |
61 |
* @return All language codes available in |
62 |
* java.util.Locale.getISOLanguages() without duplicates. |
63 |
*/ |
64 |
public static Set<String> getLanguageCodes() { |
65 |
return languageCodes; |
66 |
} |
67 |
|
68 |
/** |
69 |
* @param code |
70 |
* A two-letter language code. |
71 |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
72 |
* uses this language. |
73 |
*/ |
74 |
public static Locale getLocaleFor(String code) { |
75 |
for (Locale l : Locale.getAvailableLocales()) { |
76 |
if (l.getLanguage().equals(code.toLowerCase())) { |
77 |
return l; |
78 |
} |
79 |
// LOGGER.debug(l.getLanguage() + " not = " + code); |
80 |
} |
81 |
|
82 |
LOGGER.error("Can't create a Locale for code " + code |
83 |
+ "! Returning the system default locale to avoid NPEs."); |
84 |
|
85 |
return Locale.getDefault(); |
86 |
} |
87 |
|
88 |
/** |
89 |
* A convenience method that checks if the {@link Translation} object |
90 |
* contains a translation for the active language. A {@link String} |
91 |
* containing only spaces will return <code>false</code>. |
92 |
* |
93 |
* @param trans |
94 |
* {@link Translation} to check. |
95 |
*/ |
96 |
public static boolean isEmpty(final Translation trans) { |
97 |
if (trans == null) |
98 |
return true; |
99 |
return isEmpty(trans.toString()); |
100 |
} |
101 |
|
102 |
/** |
103 |
* A convenience method that checks if the {@link String} returned by from a |
104 |
* {@link Translation} object contains a "valid" translation for the active |
105 |
* language. A {@link String} containing only spaces or equals |
106 |
* {@link Translation}.NO_TRANSLATION will return <code>false</code>. |
107 |
* |
108 |
* @param transString |
109 |
* {@link String} to check. |
110 |
*/ |
111 |
public static boolean isEmpty(final String transString) { |
112 |
if (transString == null) |
113 |
return true; |
114 |
if (transString.trim().isEmpty()) |
115 |
return true; |
116 |
if (transString.equals(Translation.NO_TRANSLATION)) |
117 |
return true; |
118 |
return false; |
119 |
} |
120 |
} |