JSF中的本地化,如何记住每个会话而不是每个请求/视图的选定语言环境 [英] Localization in JSF, how to remember selected locale per session instead of per request/view
问题描述
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?
推荐答案
您需要将所选语言环境存储在会话范围中,并将其在viewroot中的两个位置设置:一次通过locale属性中进行一次.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/view.html"rel =" noreferrer> <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);
}
}
这是视图的一个示例,看起来应该像这样:
And here's an example of the view should look like:
<!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>
请注意,<html lang>
对于JSF的功能不是必需的,但是搜索bot解释页面的方式是必需的.否则,它可能会被标记为重复内容,这对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屋!