JSF语言环境问题

时间:2011-01-20 09:08:46

标签: java jsf localization jsf-2

我遇到这样一种情况:当用户从下拉列表中选择语言时,我希望应用程序区域设置相应地更改。捕获区域设置并不困难,但如何为之后的所有页面设置区域设置。

我在资源包和faces-config.xml

中设置了配置

3 个答案:

答案 0 :(得分:1)

注册您自己的ViewHandler并覆盖其calculateLocale()方法将是一种方法。

自定义ViewHandler需要在faces-config.xml文件中指明,例如:

<faces-config version="2.0" xmlns...>   
   <application>
           ...
       <view-handler>your.package.CustomLocaleViewHandler</view-handler> ...

并且实现仅覆盖calculateLocale()并将其他方法委托给代理ViewHandler

public class CustomLocaleViewHandler extends ViewHandler {

    private final ViewHandler base;

    public CustomLocaleViewHandler(ViewHandler base) {
        this.base = base;
    }

    @Override
    public Locale calculateLocale(FacesContext context) {
    //... your logic goes here
    return locale;
    }

    @Override
    public String calculateRenderKitId(FacesContext context) {
        return base.calculateRenderKitId(context);
    }

    @Override
    public UIViewRoot createView(FacesContext context, String viewId) {
        return base.createView(context, viewId);
    }

    ... other proxied methods

}

答案 1 :(得分:1)

我刚刚完成了这个,并且编码并不困难,但需要花一些时间来确定如何在面向未来且自动化的方式下有效地做到,但我认为我有一个合理的自动检测JSF中配置的语言,并自动将菜单项本地化为当前区域设置的方法。

首先,确保您使用某种facelets模板,否则您将不得不在您使用的每个页面上复制第一部分。使用f:view locale = parameter

设置Locale

您的网页应如下所示:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view locale="#{LocaleBean.currentLocale}">
        <h:head>
...
        </h:head>
        <h:body>
...
            <!-- Put this somewhere convenient on your page -->
            <h:form>
                <h:selectOneMenu value="#{Locale.currentLocale}" onchange="this.form.submit();" immediate="true" >
                    <f:selectItems value="#{Locale.supportedLocales}" var="loc" itemLabel="#{loc.getDisplayName(Locale.currentLocale)}" itemValue="#{loc}"/>
                    <f:converter converterId="LocaleConverter"/>
                </h:selectOneMenu>
            </h:form>
...
        </h:body>
    </f:view>
</html>

这是我的LocaleBean :(使用@ManagedBean注册,或通过faces-config.xml注册会话范围)

public class LocaleBean {

    protected List<Locale> supportedLocales = new ArrayList<Locale>();

    protected Locale currentLocale;

    /**
     * Get the value of currentLocale
     *
     * @return the value of currentLocale
     */
    public Locale getCurrentLocale() {
        if (currentLocale == null && FacesContext.getCurrentInstance() != null) {
            currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
        return currentLocale;
    }

    /**
     * Set the value of currentLocale
     *
     * @param currentLocale new value of currentLocale
     */
    public void setCurrentLocale(Locale currentLocale) {
        this.currentLocale = currentLocale;
        FacesContext.getCurrentInstance().getViewRoot().setLocale(currentLocale);
    }

    public List<Locale> getSupportedLocales() {
        if (supportedLocales.isEmpty() && FacesContext.getCurrentInstance() != null) {
            Iterator<Locale> facesLocales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
            while (facesLocales.hasNext()) {
                supportedLocales.add(facesLocales.next());
            }
        }
        return supportedLocales;
    }

    public void setSupportedLocaleStrings(Collection<String> localeStrings) {
        supportedLocales.clear();
        for (String checkLocale : localeStrings) {
            for (Locale locale : Locale.getAvailableLocales()) {
                if (locale.toString().equals(checkLocale)) {
                    supportedLocales.add(locale);
                    break;
                }
            }
        }

    }

}

LocaleConverter(再次通过注释或faces-config.xml注册)

public class LocaleConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        for (Locale locale : Locale.getAvailableLocales()) {
            if (locale.toString().equals(value)) {
                return locale;
            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value.toString();
    }

}

答案 2 :(得分:0)

我相信这就是你要找的东西:

FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(locale);