1 |
mojays |
2 |
package skrueger.i8n; |
2 |
|
|
|
3 |
alfonx |
39 |
import java.util.Locale; |
4 |
alfonx |
38 |
import java.util.Set; |
5 |
|
|
import java.util.TreeSet; |
6 |
mojays |
2 |
|
7 |
alfonx |
43 |
import org.apache.log4j.Logger; |
8 |
|
|
|
9 |
mojays |
2 |
public class I8NUtil { |
10 |
alfonx |
43 |
static final Logger LOGGER = Logger.getLogger(I8NUtil.class); |
11 |
alfonx |
39 |
|
12 |
alfonx |
38 |
private static Set<String> languageCodes = new TreeSet<String>(); |
13 |
mojays |
2 |
static { |
14 |
alfonx |
39 |
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 |
mojays |
2 |
/** |
23 |
|
|
* @author Stefan Alfons Krüger |
24 |
|
|
* @param code |
25 |
|
|
* @return true if the code paramter is a valid ISO Language code |
26 |
|
|
*/ |
27 |
|
|
public static boolean isValidISOLangCode(String code) { |
28 |
alfonx |
38 |
return getLanguageCodes().contains(code); |
29 |
mojays |
2 |
} |
30 |
alfonx |
38 |
|
31 |
|
|
/** |
32 |
alfonx |
39 |
* @return All language codes available in |
33 |
|
|
* java.util.Locale.getISOLanguages() without duplicates. |
34 |
alfonx |
38 |
*/ |
35 |
|
|
public static Set<String> getLanguageCodes() { |
36 |
|
|
return languageCodes; |
37 |
|
|
} |
38 |
alfonx |
39 |
|
39 |
|
|
/** |
40 |
|
|
* @param code |
41 |
|
|
* A two-letter language code. |
42 |
|
|
* @return <code>null</code> or one (of many possible) {@link Locale} that |
43 |
|
|
* uses this language. |
44 |
|
|
*/ |
45 |
|
|
public static Locale getLocaleFor(String code) { |
46 |
|
|
for (Locale l : Locale.getAvailableLocales()) { |
47 |
alfonx |
140 |
if (l.getLanguage().equals(code.toLowerCase())) { |
48 |
alfonx |
39 |
return l; |
49 |
|
|
} |
50 |
alfonx |
140 |
LOGGER.debug(l.getLanguage() + " not = " + code); |
51 |
alfonx |
39 |
} |
52 |
alfonx |
140 |
|
53 |
alfonx |
44 |
LOGGER.error("Can't create a Locale for code " + code |
54 |
|
|
+ "! Returning the system default locale to avoid NPEs."); |
55 |
alfonx |
140 |
|
56 |
alfonx |
43 |
return Locale.getDefault(); |
57 |
alfonx |
39 |
} |
58 |
alfonx |
44 |
|
59 |
|
|
/** |
60 |
|
|
* A convenience method that checks if the {@link Translation} object |
61 |
|
|
* contains a translation for the active language. A {@link String} |
62 |
|
|
* containing only spaces will return <code>false</code>. |
63 |
|
|
* |
64 |
|
|
* @param trans |
65 |
|
|
* {@link Translation} to check. |
66 |
|
|
*/ |
67 |
|
|
public static boolean isEmpty(final Translation trans) { |
68 |
|
|
if (trans == null) |
69 |
|
|
return true; |
70 |
|
|
return isEmpty(trans.toString()); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* A convenience method that checks if the {@link String} returned by from a |
75 |
|
|
* {@link Translation} object contains a "valid" translation for the active |
76 |
|
|
* language. A {@link String} containing only spaces or equals |
77 |
|
|
* {@link Translation}.NO_TRANSLATION will return <code>false</code>. |
78 |
|
|
* |
79 |
|
|
* @param transString |
80 |
|
|
* {@link String} to check. |
81 |
|
|
*/ |
82 |
|
|
public static boolean isEmpty(final String transString) { |
83 |
|
|
if (transString == null) |
84 |
|
|
return true; |
85 |
|
|
if (transString.trim().isEmpty()) |
86 |
|
|
return true; |
87 |
|
|
if (transString.equals(Translation.NO_TRANSLATION)) |
88 |
|
|
return true; |
89 |
|
|
return false; |
90 |
|
|
} |
91 |
mojays |
2 |
} |