1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
7 |
* |
8 |
* The SCHMITZM project is hosted at: |
9 |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
* |
11 |
* This program is free software; you can redistribute it and/or |
12 |
* modify it under the terms of the GNU Lesser General Public License |
13 |
* as published by the Free Software Foundation; either version 3 |
14 |
* of the License, or (at your option) any later version. |
15 |
* |
16 |
* This program is distributed in the hope that it will be useful, |
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* GNU General Public License for more details. |
20 |
* |
21 |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
* |
26 |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
29 |
******************************************************************************/ |
30 |
package skrueger.i8n; |
31 |
|
32 |
import java.util.List; |
33 |
import java.util.Locale; |
34 |
import java.util.Set; |
35 |
import java.util.TreeSet; |
36 |
|
37 |
import org.apache.log4j.Logger; |
38 |
|
39 |
public class I8NUtil { |
40 |
static final Logger LOGGER = Logger.getLogger(I8NUtil.class); |
41 |
|
42 |
private static Set<String> languageCodes = new TreeSet<String>(); |
43 |
static { |
44 |
for (Locale locale : java.util.Locale.getAvailableLocales()) { |
45 |
getLanguageCodes().add(locale.getLanguage()); |
46 |
} |
47 |
// for (String code : java.util.Locale.getISOLanguages()) { |
48 |
// getLanguageCodes().add(code); |
49 |
// } |
50 |
} |
51 |
|
52 |
/** |
53 |
* Creates a {@link Translation}<br/> |
54 |
* <p> |
55 |
* <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set. |
56 |
* <li>If format can't be recognized, the {@link String} is interpreted as |
57 |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
58 |
* |
59 |
* @author Stefan Alfons Krüger |
60 |
*/ |
61 |
public static Translation createFromOneLIne(final String oneLineCoded) { |
62 |
Translation result = new Translation(); |
63 |
result.fromOneLine(oneLineCoded); |
64 |
return result; |
65 |
} |
66 |
|
67 |
/** |
68 |
* Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/> |
69 |
* |
70 |
* |
71 |
* @author Stefan Alfons Krüger |
72 |
*/ |
73 |
public static String toOneLine(Translation source) { |
74 |
StringBuffer oneLine = new StringBuffer(); |
75 |
for (String key : source.keySet()) { |
76 |
oneLine.append(key + "{" + source.get(key) + "}"); |
77 |
} |
78 |
return oneLine.toString(); |
79 |
} |
80 |
|
81 |
/** |
82 |
* @author Stefan Alfons Krüger |
83 |
* @param code |
84 |
* @return true if the code paramter is a valid ISO Language code |
85 |
*/ |
86 |
public static boolean isValidISOLangCode(String code) { |
87 |
return getLanguageCodes().contains(code); |
88 |
} |
89 |
|
90 |
/** |
91 |
* @return All language codes available in |
92 |
* java.util.Locale.getISOLanguages() without duplicates. |
93 |
*/ |
94 |
public static Set<String> getLanguageCodes() { |
95 |
return languageCodes; |
96 |
} |
97 |
|
98 |
/** |
99 |
* @param code |
100 |
* A two-letter language code. |
101 |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
102 |
* uses this language. |
103 |
*/ |
104 |
public static Locale getLocaleFor(String code) { |
105 |
for (Locale l : Locale.getAvailableLocales()) { |
106 |
if (l.getLanguage().equals(code.toLowerCase())) { |
107 |
return l; |
108 |
} |
109 |
// LOGGER.debug(l.getLanguage() + " not = " + code); |
110 |
} |
111 |
|
112 |
LOGGER.error("Can't create a Locale for code " + code |
113 |
+ "! Returning the system default locale to avoid NPEs."); |
114 |
|
115 |
return Locale.getDefault(); |
116 |
} |
117 |
|
118 |
/** |
119 |
* A convenience method that checks if the {@link Translation} object |
120 |
* contains a translation for the active language. A {@link String} |
121 |
* containing only spaces will return <code>false</code>. |
122 |
* |
123 |
* @param trans |
124 |
* {@link Translation} to check. |
125 |
*/ |
126 |
public static boolean isEmpty(final Translation trans) { |
127 |
if (trans == null) |
128 |
return true; |
129 |
return isEmpty(trans.toString()); |
130 |
} |
131 |
|
132 |
/** |
133 |
* A convenience method that checks if the {@link String} returned by from a |
134 |
* {@link Translation} object contains a "valid" translation for the active |
135 |
* language. A {@link String} containing only spaces or equals |
136 |
* {@link Translation}.NO_TRANSLATION will return <code>false</code>. |
137 |
* |
138 |
* @param transString |
139 |
* {@link String} to check. |
140 |
*/ |
141 |
public static boolean isEmpty(final String transString) { |
142 |
if (transString == null) |
143 |
return true; |
144 |
if (transString.trim().isEmpty()) |
145 |
return true; |
146 |
if (transString.equals(Translation.NO_TRANSLATION)) |
147 |
return true; |
148 |
return false; |
149 |
} |
150 |
|
151 |
/** |
152 |
* @return a {@link Double} between 0 and 1 representing the part of the |
153 |
* given {@link Translation} that has been filled. |
154 |
* @param ac |
155 |
* {@link AtlasConfig} to determine the languages to expect. |
156 |
* @param trans |
157 |
* The {@link Translation} to check. |
158 |
*/ |
159 |
public static double qmTranslation(final List<String> languages, |
160 |
final Translation trans) { |
161 |
|
162 |
if (trans == null) |
163 |
return 0.; |
164 |
|
165 |
Integer cunt = 0; |
166 |
for (final String l : languages) { |
167 |
final String t = trans.get(l); |
168 |
if (!isEmpty(t)) |
169 |
cunt++; |
170 |
} |
171 |
return cunt.doubleValue() / (double) languages.size(); |
172 |
} |
173 |
} |