JSF应用程序中的动态语言环境切换? [英] Dynamic locale switching in a JSF app?

查看:142
本文介绍了JSF应用程序中的动态语言环境切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在其应用程序的欢迎页面上在语言环境之间动态切换.我看到较早的开发人员(无需大量文档继承了代码)已重写了ViewHandler的以下三种方法,并告诉我这是对Locale进行动态切换所必需的.任何帮助都将受到高度赞赏

I have an application where the user can dynamically switch between locales from the welcome page of my application. I see that the earlier developer (inherited the code without much documentation) has overridden the following three methods from ViewHandler and tells me that this is required for dynamic switching of the Locale...any help is highly appreciated

另外,请让我知道是否有更好的处理方法

Also, do let me know if there is a better way of handling this

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

推荐答案

我的解决方法是:

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

  • have a session-scoped managed-bean that holds a Locale instance
  • have the the following button (or link) for each supported language:

<h:commandButton action="#{localeBean.changeLocal}">
     <f:setPropertyActionListener target="#{localeBean.selectedLanguage}" 
              value="en" />
</h:commandButton>

  • 根据所传递的语言(new Locale(lang))设置当前语言环境

  • set the current locale based on the passed language (new Locale(lang))

    这篇关于JSF应用程序中的动态语言环境切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆