0

以此链接的 JSF2 国际化示例为起点:

http://www.mkyong.com/jsf2/jsf-2-internationalization-example/

我希望此示例以所选的实际语言显示组合的语言。

有人可以指出我如何做到这一点吗?

谢谢!

4

1 回答 1

0

只需在静态变量中设置正确的标签countries

static {
    countries = new LinkedHashMap<String,Object>();
    countries.put("English", Locale.ENGLISH); //label, value
    countries.put("Deutsch", Locale.GERMAN);
    countries.put("Français", Locale.FRENCH);
    // ... fill in with additional languages/locales as needed
}

您可以在此处以原始语言获得更大的语言名称列表:http ://www.omniglot.com/language/names.htm

更新:根据 OP 的评论,他需要将语言名称翻译成每种语言。为此,人们可以想出一个解决方案,只需制作一张语言地图(有很多地图),如下所示:

// set a default value for localeCode
private String localeCode = Locale.ENGLISH.toString();
// ...
static {
    countries = new LinkedHashMap<Object, <String,Object>>();
    englishCountries = new LinkedHashMap<String,Object>();
    englishCountries.put("English", Locale.ENGLISH); //label, value
    englishCountries.put("German", Locale.GERMAN);
    englishCountries.put("French", Locale.FRENCH);
    countries.put(Locale.ENGLISH, englishCountries);
    germanCountries = new LinkedHashMap<String,Object>();
    germanCountries.put("Englisch", Locale.ENGLISH);
    germanCountries.put("Deutsch", Locale.GERMAN);
    germanCountries.put("Französisch", Locale.FRENCH);
    countries.put(Locale.GERMAN, germanCountries);
    frenchCountries = new LinkedHashMap<String,Object>();
    frenchCountries.put("Anglais", Locale.ENGLISH);
    frenchCountries.put("Allemand", Locale.GERMAN);
    frenchCountries.put("Français", Locale.FRENCH);
    countries.put(Locale.FRENCH, frenchCountries);
    // ... fill in with additional languages/locales as needed
}

public Map<Object, <String,Object>> getCountriesInMap() {
    return countries;
}

// adapted value change listener from original:
public void countryLocaleCodeChanged(ValueChangeEvent e){
    String newLocaleValue = e.getNewValue().toString(); 
    //loop country map to compare the locale code
    for (Object key : countries.keySet()) {
        if (key.toString().equals(newLocaleValue)) {
            FacesContext.getCurrentInstance().getViewRoot()
                .setLocale((Locale) key);
        }
    }
}

然后您将选择要用于 selectItems 的正确地图,如下所示:

<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
    valueChangeListener="#{language.countryLocaleCodeChanged}">
    <f:selectItems value="#{language.countriesInMap[language.localeCode]}" /> 
</h:selectOneMenu>

注意:不要忘记为 设置默认值language.localeCode,否则下拉菜单不会显示任何选项

但是请注意,这对于可用性来说可能不是一个好主意,因为错误地选择了外语的用户可能很难恢复到她已知的语言(这就是为什么将列表具有每种自己语言的语言名称)。

于 2012-12-28T11:55:58.510 回答