【问题标题】:JSF setLocale doesn't work appropriatelyJSF setLocale 无法正常工作
【发布时间】:2011-06-12 07:47:11
【问题描述】:

我在我的 JSF 应用程序中实现了国际化,如 here 所述。

但我遇到了一个问题:当我更改语言环境时,我页面上的所有文本都发生了变化。但是,如果我单击导航链接以进入另一个页面,则区域设置会跳回标准区域设置!

我想我在这里错过了一些东西。所以我在下面提供我的代码,希望你能帮到你:

LocaleBean.java:

    @ManagedBean(name="locale")
    @SessionScoped
    public class LocaleBean {

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    public Locale getLocale() {
        return locale;
    }

    public void setLanguage(String language) {
        locale = new Locale(language);
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

    public String getLanguage() {
        return locale.getLanguage();
    }
    }

JSF 部分(它是我模板的一部分):

    <h:outputText value=" #{text['common.language']}: " /> 
        <h:selectOneMenu value="#{locale.language}" onchange="submit()">
            <f:selectItem itemValue="de" itemLabel="Deutsch" />
            <f:selectItem itemValue="en" itemLabel="English" />
        </h:selectOneMenu>

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
     <application>
           <locale-config>
                <default-locale>de</default-locale>
                <supported-locale>en</supported-locale>
           </locale-config>
       <resource-bundle>
        <base-name>org.dhbw.stg.wwi2008c.mopro.ui.text</base-name>
        <var>text</var>
       </resource-bundle>
     </application>
</faces-config>

然后我从教程中选择 Text.java,只更改了 bundle-path。

这是我的目录:

如果有什么重要的东西丢失了,请提出来。

【问题讨论】:

    标签: java jsf internationalization jsf-2


    【解决方案1】:

    FacesContext 是请求范围的实例。因此您的值将仅针对该特定请求设置。

    在 xhtml 上添加

    <f:view locale="#{locale.locale}">
    

    或:

    注册一个视图处理程序
    在 faces-config.xml 中

     <application>
               ...
           <view-handler>com.yourcompany.MyLocaleViewHandler</view-handler> 
    

    public class MyLocaleViewHandler extends ViewHandler {
    
        private final ViewHandler base;
    
    
        @Override
        public Locale calculateLocale(FacesContext context) {
          //fetch the session scoped bean and return the
          LocaleBean bean = (LocaleBean ) context.getExternalContext().getRequest().getSession().getAttribute("locale");//this line is not tested.
          return locale;
        }
          //other stuff..
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 2014-03-23
      • 2012-09-26
      • 2012-08-06
      • 2012-11-16
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多