JSF 中的本地化,如何记住每个会话而不是每个请求/视图选择的语言环境 [英] Localization in JSF, how to remember selected locale per session instead of per request/view

查看:17
本文介绍了JSF 中的本地化,如何记住每个会话而不是每个请求/视图选择的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

faces-config.xml:

<application>
    <locale-config>
        <default-locale>ru</default-locale>
        <supported-locale>ua</supported-locale>
    </locale-config>
</application> 

在 bean 操作方法中,我正在按如下方式更改当前视图中的语言环境:

In a bean action method, I'm changing the locale in the current view as follows:

FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale("ua"));

问题是应用了 ua 区域设置,但仅针对每个请求/视图而不是针对会话.同一会话中的另一个请求/视图将区域设置重置为默认的 ru 值.

The problem is that ua Locale is applied, but only per request/view and not for session. Another request/view within the same session resets the locale back to default ru value.

如何为会话应用语言环境?

How can I apply the locale for session?

推荐答案

需要将选中的 locale 存储在 session scope 中,并在 viewroot 中的两个地方设置: once by UIViewRoot#setLocale() 更改区域设置后立即(更改当前视图根的区域设置,从而反映在回发中;此后执行重定向时不需要这部分),并且在 locale 中一次的属性f:view>(在后续请求/视图中设置/保留区域设置).

You need to store the selected locale in the session scope and set it in the viewroot in two places: once by UIViewRoot#setLocale() immediately after changing the locale (which changes the locale of the current viewroot and thus get reflected in the postback; this part is not necessary when you perform a redirect afterwards) and once in the locale attribute of the <f:view> (which sets/retains the locale in the subsequent requests/views).

下面是一个 LocaleBean 应该是什么样子的例子:

Here's an example how such a LocaleBean should look like:

package com.example.faces;

import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LocaleBean {

    private Locale locale;

    @PostConstruct
    public void init() {
        locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
    }

    public Locale getLocale() {
        return locale;
    }

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

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

}

下面是一个视图示例:

<!DOCTYPE html>
<html lang="#{localeBean.language}"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<f:view locale="#{localeBean.locale}">
    <h:head>
        <title>JSF/Facelets i18n example</title>
    </h:head>
    <h:body>
        <h:form>
            <h:selectOneMenu value="#{localeBean.language}" onchange="submit()">
                <f:selectItem itemValue="en" itemLabel="English" />
                <f:selectItem itemValue="nl" itemLabel="Nederlands" />
                <f:selectItem itemValue="es" itemLabel="Español" />
            </h:selectOneMenu>
        </h:form>
        <p><h:outputText value="#{text['some.text']}" /></p>
    </h:body>
</f:view>
</html>

请注意, 不是 JSF 运行所必需的,但搜索机器人如何解释您的页面是强制性的.否则可能会被标记为重复内容,这对 SEO 不利.

Note that <html lang> is not required for functioning of JSF, but it's mandatory how search bots interpret your page. Otherwise it would possibly be marked as duplicate content which is bad for SEO.

这篇关于JSF 中的本地化,如何记住每个会话而不是每个请求/视图选择的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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