JSF 2.0 set locale throughout session from browser and programmatically(JSF 2.0 通过浏览器和编程方式在整个会话中设置语言环境)
问题描述
如何根据初始浏览器请求检测应用程序的区域设置,并在整个浏览会话期间使用它,直到用户专门更改区域设置,以及如何在剩余会话中强制使用此新区域设置?
How do I detect the locale for an application based on the initial browser request and use it throughout the browsing session untill the user specifically changes the locale and how do you force this new locale through the remaining session?
推荐答案
创建一个会话范围的托管 bean,如下所示:
Create a session scoped managed bean like follows:
@ManagedBean
@SessionScoped
public class LocaleManager {
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);
}
}
要设置视图的当前语言环境,请将其绑定到主模板的 <f:view>.
To set the current locale of the views, bind it to the <f:view> of your master template.
<f:view locale="#{localeManager.locale}">
要更改它,请将其绑定到具有语言选项的 <h:selectOneMenu>.
To change it, bind it to a <h:selectOneMenu> with language options.
<h:form>
<h:selectOneMenu value="#{localeManager.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>
要提高国际化页面的 SEO(否则会被标记为重复内容),请将语言绑定到 <html>.
To improve SEO of your internationalized pages (otherwise it would be marked as duplicate content), bind language to <html> as well.
<html lang="#{localeManager.language}">
这篇关于JSF 2.0 通过浏览器和编程方式在整个会话中设置语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSF 2.0 通过浏览器和编程方式在整个会话中设置语言环境
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
