/[schmitzm]/trunk/src/skrueger/i8n/I8NUtil.java
ViewVC logotype

Annotation of /trunk/src/skrueger/i8n/I8NUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 185 - (hide annotations)
Fri Jul 3 14:36:08 2009 UTC (15 years, 7 months ago) by alfonx
File size: 3394 byte(s)
* Un-deprecated the constructor Translation(String);

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 alfonx 185 * 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 mojays 2 * @author Stefan Alfons Krüger
30 alfonx 185 */
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 mojays 2 * @param code
54     * @return true if the code paramter is a valid ISO Language code
55     */
56     public static boolean isValidISOLangCode(String code) {
57 alfonx 38 return getLanguageCodes().contains(code);
58 mojays 2 }
59 alfonx 38
60     /**
61 alfonx 39 * @return All language codes available in
62     * java.util.Locale.getISOLanguages() without duplicates.
63 alfonx 38 */
64     public static Set<String> getLanguageCodes() {
65     return languageCodes;
66     }
67 alfonx 39
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 alfonx 140 if (l.getLanguage().equals(code.toLowerCase())) {
77 alfonx 39 return l;
78     }
79 alfonx 185 // LOGGER.debug(l.getLanguage() + " not = " + code);
80 alfonx 39 }
81 alfonx 140
82 alfonx 44 LOGGER.error("Can't create a Locale for code " + code
83     + "! Returning the system default locale to avoid NPEs.");
84 alfonx 140
85 alfonx 43 return Locale.getDefault();
86 alfonx 39 }
87 alfonx 44
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 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26