2

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

Sami
  • 2,311
  • 13
  • 46
  • 80
  • 3
    "*not workin*" is not a compiler or runtime error I have ever seen. –  May 03 '12 at 18:40
  • @house_no_name, I am surprised you have not heard about "NotWorkingException", it is the parent class of Exception, Error, UserStupidity, EndOfWorld et al. – uncaught_exceptions May 03 '12 at 19:28

2 Answers2

6

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thaks a lot, kiitos paljon! FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(localeCode)); That was so tidy and clean solution that I am shamed :) From 20 lines of code to one...There is so much to learn. Sami – Sami May 03 '12 at 22:24
2

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());
    }
}


}
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • ok, thanks. I'll try to solve it. I am using the setter straight from JSF-page like that: – Sami May 03 '12 at 21:07