svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / impl / DefaultLocaleManager.java @ 41694
History | View | Annotate | Download (14.9 KB)
1 |
package org.gvsig.andami.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.URL; |
5 |
import java.security.AccessController; |
6 |
import java.security.PrivilegedAction; |
7 |
import java.util.Collections; |
8 |
import java.util.Comparator; |
9 |
import java.util.HashSet; |
10 |
import java.util.Iterator; |
11 |
import java.util.List; |
12 |
import java.util.Locale; |
13 |
import java.util.Set; |
14 |
import java.util.TreeSet; |
15 |
import javax.swing.JComponent; |
16 |
import javax.swing.SwingUtilities; |
17 |
import org.apache.commons.configuration.PropertiesConfiguration; |
18 |
import org.apache.commons.lang3.LocaleUtils; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.gvsig.andami.Launcher; |
21 |
import org.gvsig.andami.LocaleManager; |
22 |
import org.gvsig.andami.PluginServices; |
23 |
import org.gvsig.andami.PluginsLocator; |
24 |
import org.gvsig.andami.PluginsManager; |
25 |
import org.gvsig.andami.config.generate.AndamiConfig; |
26 |
import org.gvsig.andami.installer.translations.TranslationsInstallerFactory; |
27 |
import org.gvsig.andami.ui.mdiFrame.MDIFrame; |
28 |
import org.gvsig.installer.lib.api.InstallerLocator; |
29 |
import org.gvsig.installer.lib.api.InstallerManager; |
30 |
import org.gvsig.utils.XMLEntity; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
public class DefaultLocaleManager implements LocaleManager { |
35 |
|
36 |
private static final Logger logger |
37 |
= LoggerFactory.getLogger(DefaultLocaleManager.class); |
38 |
|
39 |
private static final String LOCALE_CODE_VALENCIANO = "vl"; |
40 |
|
41 |
private static final String I18N_EXTENSION = "org.gvsig.i18n.extension"; |
42 |
private static final String INSTALLED_TRANSLATIONS_HOME_FOLDER = "i18n"; |
43 |
private static final String VARIANT = "variant"; |
44 |
private static final String COUNTRY = "country"; |
45 |
private static final String LANGUAGE = "language"; |
46 |
private static final String REGISTERED_LOCALES_PERSISTENCE = "RegisteredLocales"; |
47 |
|
48 |
private static final String LOCALE_CONFIG_FILENAME = "locale.config"; |
49 |
|
50 |
private final Locale[] hardcoredLocales = new Locale[]{ |
51 |
// Default supported locales
|
52 |
SPANISH, // Spanish
|
53 |
ENGLISH, // English
|
54 |
new Locale("en", "US"), // English US |
55 |
new Locale("ca"), // Catalan |
56 |
new Locale("gl"), // Galician |
57 |
new Locale("eu"), // Basque |
58 |
new Locale("de"), // German |
59 |
new Locale("cs"), // Czech |
60 |
new Locale("fr"), // French |
61 |
new Locale("it"), // Italian |
62 |
new Locale("pl"), // Polish |
63 |
new Locale("pt"), // Portuguese |
64 |
new Locale("pt", "BR"), // Portuguese Brazil |
65 |
new Locale("ro"), // Romanian |
66 |
new Locale("zh"), // Chinese |
67 |
new Locale("ru"), // Russian |
68 |
new Locale("el"), // Greek |
69 |
new Locale("ro"), // Romanian |
70 |
new Locale("pl"), // Polish |
71 |
new Locale("tr"), // Turkish |
72 |
new Locale("sr"), // Serbio |
73 |
new Locale("sw") // Swahili |
74 |
}; |
75 |
|
76 |
private Set installedLocales = null; |
77 |
private Set defaultLocales = null; |
78 |
|
79 |
public DefaultLocaleManager() {
|
80 |
Locale currentLocale = org.gvsig.i18n.Messages.getCurrentLocale();
|
81 |
this.setCurrentLocale(currentLocale);
|
82 |
} |
83 |
|
84 |
public Locale getCurrentLocale() { |
85 |
return org.gvsig.i18n.Messages.getCurrentLocale();
|
86 |
} |
87 |
|
88 |
public void setCurrentLocale(final Locale locale) { |
89 |
org.gvsig.i18n.Messages.setCurrentLocale(locale, this.getLocaleAlternatives(locale));
|
90 |
|
91 |
String localeStr = locale.getLanguage();
|
92 |
if (localeStr.equalsIgnoreCase(LOCALE_CODE_VALENCIANO)) {
|
93 |
Locale.setDefault(new Locale("ca")); |
94 |
} else {
|
95 |
Locale.setDefault(locale);
|
96 |
} |
97 |
|
98 |
AndamiConfig config = Launcher.getAndamiConfig(); |
99 |
config.setLocaleLanguage(locale.getLanguage()); |
100 |
config.setLocaleCountry(locale.getCountry()); |
101 |
config.setLocaleVariant(locale.getVariant()); |
102 |
|
103 |
setCurrentLocaleUI(locale); |
104 |
} |
105 |
|
106 |
public Locale getDefaultSystemLocale() { |
107 |
String language, region, country, variant;
|
108 |
language = getSystemProperty("user.language", "en"); |
109 |
// for compatibility, check for old user.region property
|
110 |
region = getSystemProperty("user.region", null); |
111 |
|
112 |
if (region != null) { |
113 |
// region can be of form country, country_variant, or _variant
|
114 |
int i = region.indexOf('_'); |
115 |
if (i >= 0) { |
116 |
country = region.substring(0, i);
|
117 |
variant = region.substring(i + 1);
|
118 |
} else {
|
119 |
country = region; |
120 |
variant = "";
|
121 |
} |
122 |
} else {
|
123 |
country = getSystemProperty("user.country", ""); |
124 |
variant = getSystemProperty("user.variant", ""); |
125 |
} |
126 |
return new Locale(language, country, variant); |
127 |
} |
128 |
|
129 |
private Set<Locale> loadLocalesFromConfing(File localesFile) { |
130 |
PropertiesConfiguration config = null;
|
131 |
if (!localesFile.canRead()) {
|
132 |
return null; |
133 |
} |
134 |
try {
|
135 |
config = new PropertiesConfiguration(localesFile);
|
136 |
} catch (Exception ex) { |
137 |
logger.warn("Can't open locale configuration '" + localesFile + "'.", ex); |
138 |
return null; |
139 |
} |
140 |
|
141 |
Set<Locale> locales = new HashSet<Locale>(); |
142 |
List localeCodes = config.getList("locale"); |
143 |
for (Object localeCode : localeCodes) { |
144 |
try {
|
145 |
Locale locale = LocaleUtils.toLocale((String) localeCode); |
146 |
locales.add(locale); |
147 |
} catch(IllegalArgumentException ex) { |
148 |
logger.warn("Can't load locale '"+localeCode+"' from config '"+localesFile.getAbsolutePath()+"'.",ex); |
149 |
} |
150 |
} |
151 |
return locales;
|
152 |
} |
153 |
|
154 |
public Set<Locale> getDefaultLocales() { |
155 |
if (this.defaultLocales == null) { |
156 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
157 |
File localesFile = new File(pluginsManager.getApplicationI18nFolder(), LOCALE_CONFIG_FILENAME); |
158 |
|
159 |
Set<Locale> locales = loadLocalesFromConfing(localesFile); |
160 |
defaultLocales = new HashSet<Locale>(); |
161 |
if( locales == null ) { |
162 |
for (int i = 0; i < hardcoredLocales.length; i++) { |
163 |
defaultLocales.add(hardcoredLocales[i]); |
164 |
} |
165 |
} else {
|
166 |
defaultLocales.addAll(locales); |
167 |
} |
168 |
} |
169 |
return this.defaultLocales; |
170 |
} |
171 |
|
172 |
public boolean installLocales(URL localesFile) { |
173 |
PluginsManager pluginsManager = PluginsLocator.getManager(); |
174 |
PropertiesConfiguration config = null;
|
175 |
try {
|
176 |
config = new PropertiesConfiguration(localesFile);
|
177 |
} catch (Exception ex) { |
178 |
logger.warn("Can't open configuration '" + localesFile + "'.", ex); |
179 |
} |
180 |
if (config == null) { |
181 |
return false; |
182 |
} |
183 |
List localeCodes = config.getList("locale"); |
184 |
for (Object localeCode : localeCodes) { |
185 |
Locale locale = LocaleUtils.toLocale((String) localeCode); |
186 |
if (!getInstalledLocales().contains(locale)) {
|
187 |
this.installedLocales.add(locale);
|
188 |
} |
189 |
} |
190 |
storeInstalledLocales(); |
191 |
return true; |
192 |
} |
193 |
|
194 |
public Set<Locale> getInstalledLocales() { |
195 |
if (installedLocales == null) { |
196 |
installedLocales = new TreeSet<Locale>(new Comparator<Locale>() { |
197 |
public int compare(Locale t0, Locale t1) { |
198 |
if (t1 == null || t0 == null) { |
199 |
return 0; |
200 |
} |
201 |
// return getLanguageDisplayName(t0).compareToIgnoreCase(getLanguageDisplayName(t1));
|
202 |
return t0.toString().compareToIgnoreCase(t1.toString());
|
203 |
} |
204 |
}); |
205 |
|
206 |
XMLEntity child = getRegisteredLocalesPersistence(); |
207 |
|
208 |
// If the list of registered locales is not already persisted,
|
209 |
// this should be the first time gvSIG is run with the I18nPlugin
|
210 |
// so we will take the list of default locales
|
211 |
if (child == null) { |
212 |
installedLocales.addAll(getDefaultLocales()); |
213 |
storeInstalledLocales(); |
214 |
} else {
|
215 |
XMLEntity localesEntity = getRegisteredLocalesPersistence(); |
216 |
for (int i = 0; i < localesEntity.getChildrenCount(); i++) { |
217 |
Locale locale = null; |
218 |
XMLEntity localeEntity = localesEntity.getChild(i); |
219 |
String language = localeEntity.getStringProperty(LANGUAGE);
|
220 |
String country = localeEntity.getStringProperty(COUNTRY);
|
221 |
String variant = localeEntity.getStringProperty(VARIANT);
|
222 |
locale = new Locale(language, country, variant); |
223 |
installedLocales.add(locale); |
224 |
} |
225 |
} |
226 |
Set<Locale> lfp = getAllLocalesFromPackages(); |
227 |
installedLocales.addAll(lfp); |
228 |
} |
229 |
return Collections.unmodifiableSet(installedLocales); |
230 |
} |
231 |
|
232 |
public boolean installLocale(Locale locale) { |
233 |
// Add the locale to the list of installed ones, if it
|
234 |
// is new, otherwise, update the texts.
|
235 |
if (!getInstalledLocales().contains(locale)) {
|
236 |
getInstalledLocales().add(locale); |
237 |
storeInstalledLocales(); |
238 |
} |
239 |
return true; |
240 |
} |
241 |
|
242 |
public boolean uninstallLocale(Locale locale) { |
243 |
if (!getInstalledLocales().remove(locale)) {
|
244 |
return false; |
245 |
} |
246 |
storeInstalledLocales(); |
247 |
return true; |
248 |
} |
249 |
|
250 |
public String getLanguageDisplayName(Locale locale) { |
251 |
|
252 |
String displayName;
|
253 |
|
254 |
if ("eu".equals(locale.getLanguage()) |
255 |
&& "vascuence".equals(locale.getDisplayLanguage())) {
|
256 |
// Correction for the Basque language display name,
|
257 |
// show it in Basque, not in Spanish
|
258 |
displayName = "Euskera";
|
259 |
} else if (LOCALE_CODE_VALENCIANO.equals(locale.getLanguage())) { |
260 |
// Patch for Valencian/Catalan
|
261 |
displayName = "Valenci?";
|
262 |
} else {
|
263 |
displayName = locale.getDisplayLanguage(locale); |
264 |
} |
265 |
|
266 |
return StringUtils.capitalize(displayName);
|
267 |
} |
268 |
|
269 |
public Locale[] getLocaleAlternatives(Locale locale) { |
270 |
Locale alternatives[] = null; |
271 |
|
272 |
String localeStr = locale.getLanguage();
|
273 |
if (localeStr.equals("es") |
274 |
|| localeStr.equals("ca")
|
275 |
|| localeStr.equals("gl")
|
276 |
|| localeStr.equals("eu")
|
277 |
|| localeStr.equals(LOCALE_CODE_VALENCIANO)) { |
278 |
alternatives = new Locale[2]; |
279 |
alternatives[0] = new Locale("es"); |
280 |
alternatives[1] = new Locale("en"); |
281 |
} else {
|
282 |
// prefer English for the rest
|
283 |
alternatives = new Locale[2]; |
284 |
alternatives[0] = new Locale("en"); |
285 |
alternatives[1] = new Locale("es"); |
286 |
} |
287 |
return alternatives;
|
288 |
} |
289 |
|
290 |
private void setCurrentLocaleUI(final Locale locale) { |
291 |
if (!SwingUtilities.isEventDispatchThread()) { |
292 |
try {
|
293 |
SwingUtilities.invokeAndWait(new Runnable() { |
294 |
public void run() { |
295 |
setCurrentLocaleUI(locale); |
296 |
} |
297 |
}); |
298 |
} catch (Exception ex) { |
299 |
// Ignore
|
300 |
} |
301 |
} |
302 |
try {
|
303 |
JComponent.setDefaultLocale(locale);
|
304 |
} catch (Exception ex) { |
305 |
logger.warn("Problems setting locale to JComponent.", ex);
|
306 |
} |
307 |
if (MDIFrame.isInitialized()) {
|
308 |
try {
|
309 |
MDIFrame.getInstance().setLocale(locale); |
310 |
} catch (Exception ex) { |
311 |
logger.warn("Problems settings locale to MDIFrame.", ex);
|
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
public File getResourcesFolder() { |
317 |
File i18nFolder = new File( |
318 |
PluginsLocator.getManager().getApplicationHomeFolder(), |
319 |
INSTALLED_TRANSLATIONS_HOME_FOLDER |
320 |
); |
321 |
if (!i18nFolder.exists()) {
|
322 |
i18nFolder.mkdirs(); |
323 |
} |
324 |
return i18nFolder;
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* Returns the child XMLEntity with the RegisteredLocales.
|
329 |
*/
|
330 |
private XMLEntity getRegisteredLocalesPersistence() {
|
331 |
XMLEntity entity = getI18nPersistence(); |
332 |
XMLEntity child = null;
|
333 |
for (int i = entity.getChildrenCount() - 1; i >= 0; i--) { |
334 |
XMLEntity tmpchild = entity.getChild(i); |
335 |
if (tmpchild.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) {
|
336 |
child = tmpchild; |
337 |
break;
|
338 |
} |
339 |
} |
340 |
return child;
|
341 |
} |
342 |
|
343 |
/**
|
344 |
* Stores the list of installed locales into the plugin persistence.
|
345 |
*/
|
346 |
private void storeInstalledLocales() { |
347 |
XMLEntity localesEntity = getRegisteredLocalesPersistence(); |
348 |
|
349 |
// Remove the previous list of registered languages
|
350 |
if (localesEntity != null) { |
351 |
XMLEntity i18nPersistence = getI18nPersistence(); |
352 |
for (int i = i18nPersistence.getChildrenCount() - 1; i >= 0; i--) { |
353 |
XMLEntity child = i18nPersistence.getChild(i); |
354 |
if (child.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) {
|
355 |
i18nPersistence.removeChild(i); |
356 |
break;
|
357 |
} |
358 |
} |
359 |
} |
360 |
|
361 |
// Create the new persistence for the registered languages
|
362 |
localesEntity = new XMLEntity();
|
363 |
|
364 |
localesEntity.setName(REGISTERED_LOCALES_PERSISTENCE); |
365 |
|
366 |
Set<Locale> locales = getInstalledLocales(); |
367 |
for (Iterator iterator = locales.iterator(); iterator.hasNext();) { |
368 |
Locale locale = (Locale) iterator.next(); |
369 |
XMLEntity localeEntity = new XMLEntity();
|
370 |
localeEntity.setName(locale.getDisplayName()); |
371 |
localeEntity.putProperty(LANGUAGE, locale.getLanguage()); |
372 |
localeEntity.putProperty(COUNTRY, locale.getCountry()); |
373 |
localeEntity.putProperty(VARIANT, locale.getVariant()); |
374 |
|
375 |
localesEntity.addChild(localeEntity); |
376 |
} |
377 |
|
378 |
getI18nPersistence().addChild(localesEntity); |
379 |
} |
380 |
|
381 |
/**
|
382 |
* Returns the I18n Plugin persistence.
|
383 |
*/
|
384 |
private XMLEntity getI18nPersistence() {
|
385 |
XMLEntity entity |
386 |
= PluginServices.getPluginServices(I18N_EXTENSION).getPersistentXML(); |
387 |
return entity;
|
388 |
} |
389 |
|
390 |
@SuppressWarnings("unchecked") |
391 |
private String getSystemProperty(final String property, String defaultValue) { |
392 |
String value
|
393 |
= (String) AccessController.doPrivileged(new PrivilegedAction() { |
394 |
public Object run() { |
395 |
return System.getProperty(property); |
396 |
} |
397 |
}); |
398 |
return value == null ? defaultValue : value; |
399 |
} |
400 |
|
401 |
private Set<Locale> getAllLocalesFromPackages() { |
402 |
Set<Locale> locales = new HashSet<Locale>(); |
403 |
InstallerManager installerManager = InstallerLocator.getInstallerManager(); |
404 |
List<File> folders = installerManager.getAddonFolders(TranslationsInstallerFactory.PROVIDER_NAME); |
405 |
for( File folder : folders ) { |
406 |
File file = new File(folder,LOCALE_CONFIG_FILENAME); |
407 |
Set<Locale> l = this.loadLocalesFromConfing(file); |
408 |
locales.addAll(l); |
409 |
} |
410 |
return locales;
|
411 |
} |
412 |
} |