Question

Could someone tell me why locale Finnish is not working and the rest are?

private static Map<String,Object> countries;
    private static Locale finnishLocale = new Locale("fi", "FI");

static{
    countries = new LinkedHashMap<String,Object>();
    countries.put("English", Locale.ENGLISH); //label, value
    countries.put("French", Locale.FRENCH);
    countries.put("German", Locale.GERMAN);
    countries.put("Finnish", finnishLocale); <---------- Not working!
}

public void setLocaleCode(String localeCode) {


                this.localeCode = localeCode;
                updateLocale(localeCode);          


        }


public void updateLocale(String newLocale){

        String newLocaleValue = newLocale;

        //loop country map to compare the locale code
                for (Map.Entry<String, Object> entry : countries.entrySet()) {

               if(entry.getValue().toString().equals(newLocaleValue)){

                FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale((Locale)entry.getValue());

              }
               }

        }

I mean that the locale which I created with New-clause is not working. I can't explain it better, because I thought that locale implemented like that is similar Locale-object than for instance Locale.GERMAN? My software doesn't do anything else than update locale ja Faces context. There is no exceptions. Sorry if the q is stupid. Everything else is working, I mean German, English etc and the program updates the locale and Faces context.

I would be very much appreciated if you answer the question, I am lost (again) Sami

Was it helpful?

Solution

Your updateLocale() method seems to be the culprit. You're comparing Locale#toString() against newLocale. The Locale constants have all only the language set, not the country. Locale.ENGLISH.toString() for example returns "en" while new Locale("fi", "FI").toString() returns "fi_FI". That can only mean that your newLocale variable actually contains "en", "fr", "de" and "fi". The first three would match the constants, but the latter wouldn't match the finnishLocale, because you're comparing against its toString() instead of getLanguage().

To fix your problem, either change

private static Locale finnishLocale = new Locale("fi", "FI");

to

private static Locale finnishLocale = new Locale("fi");

or, better, change Map<String, Object> to Map<String, Locale> and then change

if(entry.getValue().toString().equals(newLocaleValue)){

to

if(entry.getValue().getLanguage().equals(newLocaleValue)){

All with all, this map loop is rather clumsy. If the newLocale is a server side controlled value, just do viewRoot.setLocale(new Locale(newLocale)) instead.

OTHER TIPS

You have an error somewhere else since this works:

public class Runner01 {
private static Map<String,Object> countries;
private static Locale finnishLocale = new Locale("fi", "FI");

static{
    countries = new LinkedHashMap<String,Object>();
    countries.put("English", Locale.ENGLISH); //label, value
    countries.put("French", Locale.FRENCH);
    countries.put("German", Locale.GERMAN);
    countries.put("Finnish", finnishLocale); 
}

public static void main(String[] args) {
    for( Map.Entry<String, Object> entry : countries.entrySet() ) {
        System.out.println(entry.getKey() + "=>" + entry.getValue().toString());
    }
}


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top