JSF应用程序中的动态区域设置切换?

Ara*_*ram 4 jsf locale internationalization

我有一个应用程序,用户可以在我的应用程序的欢迎页面之间动态切换区域设置.我看到早期的开发人员(继承了没有太多文档的代码)已经从ViewHandler中覆盖了以下三个方法,并告诉我这是动态切换Locale所必需的...任何帮助都非常感谢

另外,如果有更好的方法可以让我知道

public Locale calculateLocale(FacesContext facescontext)
{
    Locale userLocale = LocaleManager.getInstance().getCurrentLocale();
    if (userLocale != null)
    {
        return userLocale;
    }
    else
    {
        return delegate.calculateLocale(facescontext);
    }
}

public void renderView(FacesContext facescontext, UIViewRoot uiviewroot)
        throws IOException, FacesException {
    uiviewroot.setLocale(LocaleManager.getInstance().getCurrentLocale());
    delegate.renderView(facescontext, uiviewroot);
}
public UIViewRoot createView(FacesContext facescontext, String s)
{
    UIViewRoot view = delegate.createView(facescontext, s);
    view.setLocale(LocaleManager.getInstance().getCurrentLocale());
    return view;
}
Run Code Online (Sandbox Code Playgroud)

Boz*_*zho 9

我的解决方案是:

  • 有一个会话范围的托管bean,其中包含一个 Locale实例
  • 为每种支持的语言提供以下按钮(或链接):

    <h:commandButton action="#{localeBean.changeLocal}">
         <f:setPropertyActionListener target="#{localeBean.selectedLanguage}" 
                  value="en" />
    </h:commandButton>
    
    Run Code Online (Sandbox Code Playgroud)
  • 根据传递的语言设置当前语言环境(new Locale(lang))

  • 在您的模板中使用 <f:view locale="#{localeBean.currentLocale}">