[英]JSF setLocale doesn't work appropriately
我在這里描述的我的JSF應用程序中實現了國際化。
但是我遇到了一個問題:當我更改語言環境時,我頁面上的所有文本都會發生變化。 但是如果我點擊導航鏈接到另一個頁面,那么語言環境會跳回到標准語言環境!
我想我在這里想念一些東西。 所以我在下面提供了我的代碼,希望你能提供幫助:
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 Part(它是我模板的一部分):
<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。
這是我的目錄:
如果遺漏了重要的東西請求。
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..
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.