Revision 42184

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultLocaleManager.java
12 12
import java.util.Locale;
13 13
import java.util.Set;
14 14
import java.util.TreeSet;
15

  
15 16
import javax.swing.JComponent;
16 17
import javax.swing.SwingUtilities;
18

  
17 19
import org.apache.commons.configuration.PropertiesConfiguration;
18 20
import org.apache.commons.lang3.LocaleUtils;
19 21
import org.apache.commons.lang3.StringUtils;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

  
20 25
import org.gvsig.andami.Launcher;
21 26
import org.gvsig.andami.LocaleManager;
22 27
import org.gvsig.andami.PluginServices;
......
28 33
import org.gvsig.installer.lib.api.InstallerLocator;
29 34
import org.gvsig.installer.lib.api.InstallerManager;
30 35
import org.gvsig.utils.XMLEntity;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33 36

  
34 37
public class DefaultLocaleManager implements LocaleManager {
35 38

  
......
44 47
    private static final String COUNTRY = "country";
45 48
    private static final String LANGUAGE = "language";
46 49
    private static final String REGISTERED_LOCALES_PERSISTENCE = "RegisteredLocales";
47
    
50

  
48 51
    private static final String LOCALE_CONFIG_FILENAME = "locale.config";
49 52

  
50 53
    private final Locale[] hardcoredLocales = new Locale[]{
......
76 79
    private Set installedLocales = null;
77 80
    private Set defaultLocales = null;
78 81

  
82
    private boolean storedInstalledLocales = false;
83

  
79 84
    public DefaultLocaleManager() {
80 85
        Locale currentLocale = org.gvsig.i18n.Messages.getCurrentLocale();
81 86
        this.setCurrentLocale(currentLocale);
......
86 91
    }
87 92

  
88 93
    public void setCurrentLocale(final Locale locale) {
89
        org.gvsig.i18n.Messages.setCurrentLocale(locale, this.getLocaleAlternatives(locale));
94
        Locale nearestLocale = getNearestLocale(locale);
95
        if(nearestLocale == null){
96
            nearestLocale = this.getLocaleAlternatives(locale)[0];
97
        }
98
        org.gvsig.i18n.Messages.setCurrentLocale(nearestLocale, this.getLocaleAlternatives(nearestLocale));
90 99

  
91
        String localeStr = locale.getLanguage();
100
        String localeStr = nearestLocale.getLanguage();
92 101
        if (localeStr.equalsIgnoreCase(LOCALE_CODE_VALENCIANO)) {
93 102
            Locale.setDefault(new Locale("ca"));
94 103
        } else {
95
            Locale.setDefault(locale);
104
            Locale.setDefault(nearestLocale);
96 105
        }
97 106

  
98 107
        AndamiConfig config = Launcher.getAndamiConfig();
99
        config.setLocaleLanguage(locale.getLanguage());
100
        config.setLocaleCountry(locale.getCountry());
101
        config.setLocaleVariant(locale.getVariant());
108
        config.setLocaleLanguage(nearestLocale.getLanguage());
109
        config.setLocaleCountry(nearestLocale.getCountry());
110
        config.setLocaleVariant(nearestLocale.getVariant());
102 111

  
103
        setCurrentLocaleUI(locale);
112
        setCurrentLocaleUI(nearestLocale);
104 113
    }
105 114

  
106 115
    public Locale getDefaultSystemLocale() {
......
126 135
        return new Locale(language, country, variant);
127 136
    }
128 137

  
129
    private Set<Locale> loadLocalesFromConfing(File localesFile)  { 
138
    private Set<Locale> loadLocalesFromConfing(File localesFile)  {
130 139
        PropertiesConfiguration config = null;
131 140
        if (!localesFile.canRead()) {
132 141
            return null;
......
137 146
            logger.warn("Can't open locale configuration '" + localesFile + "'.", ex);
138 147
            return null;
139 148
        }
140
        
149

  
141 150
        Set<Locale> locales = new HashSet<Locale>();
142 151
        List localeCodes = config.getList("locale");
143 152
        for (Object localeCode : localeCodes) {
......
150 159
        }
151 160
        return locales;
152 161
    }
153
    
162

  
154 163
    public Set<Locale> getDefaultLocales() {
155 164
        if (this.defaultLocales == null) {
156 165
            PluginsManager pluginsManager = PluginsLocator.getManager();
157 166
            File localesFile = new File(pluginsManager.getApplicationI18nFolder(), LOCALE_CONFIG_FILENAME);
158
            
167

  
159 168
            Set<Locale> locales = loadLocalesFromConfing(localesFile);
160 169
            defaultLocales = new HashSet<Locale>();
161 170
            if( locales == null ) {
......
210 219
            // so we will take the list of default locales
211 220
            if (child == null) {
212 221
                installedLocales.addAll(getDefaultLocales());
213
                storeInstalledLocales();
214 222
            } else {
215
                XMLEntity localesEntity = getRegisteredLocalesPersistence();
223
                XMLEntity localesEntity = child;
216 224
                for (int i = 0; i < localesEntity.getChildrenCount(); i++) {
217 225
                    Locale locale = null;
218 226
                    XMLEntity localeEntity = localesEntity.getChild(i);
......
226 234
            Set<Locale> lfp = getAllLocalesFromPackages();
227 235
            installedLocales.addAll(lfp);
228 236
        }
237
        storeInstalledLocales();
229 238
        return Collections.unmodifiableSet(installedLocales);
230 239
    }
231 240

  
......
265 274

  
266 275
        return StringUtils.capitalize(displayName);
267 276
    }
268
    
277

  
269 278
    public String getLocaleDisplayName(Locale locale) {
270 279
        String displayName = this.getLanguageDisplayName(locale);
271
        
280

  
272 281
        if( !StringUtils.isBlank(locale.getCountry()) ) {
273 282
            if( !StringUtils.isBlank(locale.getVariant()) ) {
274
                displayName = displayName + " (" + 
275
                        StringUtils.capitalize(locale.getDisplayCountry(locale)) + "/" + 
283
                displayName = displayName + " (" +
284
                        StringUtils.capitalize(locale.getDisplayCountry(locale)) + "/" +
276 285
                        locale.getDisplayVariant(locale) + ")";
277 286
            } else {
278 287
                displayName = displayName + " ("+locale.getDisplayCountry(locale)+")";
279 288
            }
280 289
        }
281
        
290

  
282 291
        return displayName;
283 292
    }
284 293

  
......
345 354
     */
346 355
    private XMLEntity getRegisteredLocalesPersistence() {
347 356
        XMLEntity entity = getI18nPersistence();
357
        if (entity == null) {
358
            return null;
359
        }
348 360
        XMLEntity child = null;
349 361
        for (int i = entity.getChildrenCount() - 1; i >= 0; i--) {
350 362
            XMLEntity tmpchild = entity.getChild(i);
......
360 372
     * Stores the list of installed locales into the plugin persistence.
361 373
     */
362 374
    private void storeInstalledLocales() {
375
        if(this.storedInstalledLocales ){
376
            return;
377
        }
378
        XMLEntity i18nPersistence = getI18nPersistence();
379
        if(i18nPersistence == null){
380
            return;
381
        }
363 382
        XMLEntity localesEntity = getRegisteredLocalesPersistence();
364 383

  
365 384
        // Remove the previous list of registered languages
366 385
        if (localesEntity != null) {
367
            XMLEntity i18nPersistence = getI18nPersistence();
368 386
            for (int i = i18nPersistence.getChildrenCount() - 1; i >= 0; i--) {
369 387
                XMLEntity child = i18nPersistence.getChild(i);
370 388
                if (child.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) {
......
379 397

  
380 398
        localesEntity.setName(REGISTERED_LOCALES_PERSISTENCE);
381 399

  
382
        Set<Locale> locales = getInstalledLocales();
400
        Set<Locale> locales = this.installedLocales; //getInstalledLocales();
383 401
        for (Iterator iterator = locales.iterator(); iterator.hasNext();) {
384 402
            Locale locale = (Locale) iterator.next();
385 403
            XMLEntity localeEntity = new XMLEntity();
......
392 410
        }
393 411

  
394 412
        getI18nPersistence().addChild(localesEntity);
413
        this.storedInstalledLocales = true;
395 414
    }
396 415

  
397 416
    /**
398 417
     * Returns the I18n Plugin persistence.
399 418
     */
400 419
    private XMLEntity getI18nPersistence() {
401
        XMLEntity entity
402
                = PluginServices.getPluginServices(I18N_EXTENSION).getPersistentXML();
403
        return entity;
420
        try {
421
            XMLEntity entity = PluginServices.getPluginServices(I18N_EXTENSION).getPersistentXML();
422
            return entity;
423
        } catch (Throwable t) {
424
            // logger.warn("Can't get I18nPersistence",t);
425
            return null;
426
        }
404 427
    }
405 428

  
406 429
    @SuppressWarnings("unchecked")
......
413 436
                });
414 437
        return value == null ? defaultValue : value;
415 438
    }
416
    
439

  
417 440
    private Set<Locale> getAllLocalesFromPackages() {
418 441
        Set<Locale> locales = new HashSet<Locale>();
419 442
        InstallerManager installerManager = InstallerLocator.getInstallerManager();
......
425 448
        }
426 449
        return locales;
427 450
    }
451

  
452
    public Locale getNearestLocale(Locale locale) {
453
        String language = locale.getLanguage();
454
        String country = locale.getCountry();
455
        String variant = locale.getVariant();
456
        Locale nearestLocale;
457
        if (language != null && !language.isEmpty()) {
458
            if (country != null && !country.isEmpty()) {
459
                if(variant != null && !variant.isEmpty()){
460
                    nearestLocale = new Locale(language, country, variant);
461
                    if (getInstalledLocales().contains(nearestLocale)) {
462
                        return nearestLocale;
463
                    }
464
                }
465
                nearestLocale = new Locale(language, country);
466
                if (getInstalledLocales().contains(nearestLocale)) {
467
                    return nearestLocale;
468
                }
469
            }
470
            nearestLocale = new Locale(language);
471
            if (getInstalledLocales().contains(nearestLocale)) {
472
                return nearestLocale;
473
            }
474
        }
475
        return null;
476
    }
428 477
}
trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/LocaleManager.java
24 24
    public boolean installLocale(Locale locale);
25 25

  
26 26
    public boolean uninstallLocale(Locale locale);
27
    
27

  
28 28
    public boolean installLocales(URL localesFile);
29 29

  
30 30
    public File getResourcesFolder();
31
    
31

  
32 32
    public Locale[] getLocaleAlternatives(Locale locale);
33
    
33

  
34 34
    public String getLanguageDisplayName(Locale locale);
35
    
35

  
36 36
    public String getLocaleDisplayName(Locale locale);
37

  
38
    public Locale getNearestLocale(Locale locale);
37 39
}

Also available in: Unified diff