2

有人可以告诉我为什么语言环境芬兰语不起作用而其余的都起作用吗?

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

              }
               }

        }

我的意思是我用 New-clause 创建的语言环境不起作用。我无法更好地解释它,因为我认为这样实现的语言环境与例如 Locale.GERMAN 类似的语言环境对象?我的软件除了更新语言环境 ja Faces 上下文之外什么都不做。没有例外。对不起,如果q是愚蠢的。其他一切正常,我的意思是德语、英语等,并且程序会更新语言环境和 Faces 上下文。

如果您回答这个问题,我将不胜感激,我(再次)迷路了萨米

4

2 回答 2

6

你的updateLocale()方法似乎是罪魁祸首。您正在Locale#toString()newLocale. Locale常量只有语言集,而不是国家。Locale.ENGLISH.toString()例如返回"en"new Locale("fi", "FI").toString()返回"fi_FI"。这只能意味着您的变量newLocale实际上包含"en""fr"和。前三个将匹配常量,但后者不会匹配,因为您正在与它进行比较而不是."de""fi"finnishLocaletoString()getLanguage()

要解决您的问题,更改

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

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

或者,更好的是,更改Map<String, Object>Map<String, Locale>然后更改

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

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

总而言之,这个地图循环相当笨拙。如果newLocale是服务器端控制的值,则viewRoot.setLocale(new Locale(newLocale))改为执行。

于 2012-05-03T21:04:35.770 回答
2

由于此方法有效,因此您在其他地方有错误:

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


}
于 2012-05-03T21:03:40.497 回答