<默认语言环境>似乎不起作用,而是 <supported-locale>正在使用 [英] <default-locale> does not seem to work, instead the <supported-locale> is being used
问题描述
我想将我的 jsf web 项目的默认位置设置为英文,所以我做了以下事情:
faces-config.xml
<应用程序><语言环境配置><default-locale>en_EN</default-locale><supported-locale>de_DE</supported-locale></locale-config><资源包><base-name>de.hof.tschuwwa.resources.messages</base-name><var>消息</var></资源包></应用程序>
我的 src 文件夹 de.hof.tschuwwa.resources
和里面有一个包在那个包中,我有两个属性文件:
messages_de_DE.propertiesmessages_en_EN.properties
我在一个小的 xhtml 文件中对其进行了测试:
<h:body><ui:composition template="/templates/master.xhtml"><ui:define name="header"><h:form><h:commandButton action="#{navigationService.toLogin}" value="#{msg['login']}"/><h:commandButton action="#{navigationService.toRegister}" value="#{msg['register']}"/></h:form></ui:define></ui:composition></h:身体>
当我启动我的应用程序时,它显示的是德国价值观,而不是英语价值观.我希望默认语言是英语.
如何设置我的默认语言?faces-config.xml 默认语言环境好像没用.
我还需要知道如何更改本地化.我写了这个小服务:
@Named@SessionScoped公共类 LanguageService 实现 Serializable {私有静态最终长序列版本UID = 1L;公共无效changeLanguage(字符串语言){FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language));}}
并尝试在表单中使用以下按钮:
<p:commandButton id="english" value="English" action="#{languageService.changeLanguage('en')}" immediate="true"/><p:commandButton id="german" value="Deutsch" action="#{languageService.changeLanguage('de')}" immediate="true"/>
但是当我使用它们时什么也没发生.
<default-locale>en_EN</default-locale>
首先,您在语言环境标识符 en_EN
中存在错误.前两个小写字符代表 .
i wanted to set the default location of my jsf web project to english, so i did the following:
faces-config.xml
<application>
<locale-config>
<default-locale>en_EN</default-locale>
<supported-locale>de_DE</supported-locale>
</locale-config>
<resource-bundle>
<base-name>de.hof.tschuwwa.resources.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
i have a package in my src folder de.hof.tschuwwa.resources
and inside
of that package i have two property files:
messages_de_DE.properties
messages_en_EN.properties
and i tested it in a small xhtml file:
<h:body>
<ui:composition template="/templates/master.xhtml">
<ui:define name="header">
<h:form>
<h:commandButton action="#{navigationService.toLogin}" value="#{msg['login']}" />
<h:commandButton action="#{navigationService.toRegister}" value="#{msg['register']}" />
</h:form>
</ui:define>
</ui:composition>
</h:body>
When i start my application it shows the german values and not the english values. i wanted the default language to be english.
How can i set my default language? The faces-config.xml default-locale does not seem to work.
Also i need to know how i can change my localization. i wrote this small service:
@Named
@SessionScoped
public class LanguageService implements Serializable {
private static final long serialVersionUID = 1L;
public void changeLanguage(String language) {
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language));
}
}
and tried out with the following buttons inside a form:
<p:commandButton id="english" value="English" action="#{languageService.changeLanguage('en')}" immediate="true" />
<p:commandButton id="german" value="Deutsch" action="#{languageService.changeLanguage('de')}" immediate="true" />
but nothing happened when i used them.
<default-locale>en_EN</default-locale>
First of all, you've there a mistake in the locale identifier en_EN
. The first two lowercase characters represent the ISO 693-1 language code. It is optional and only required whenever the country code is present. The last two uppercased characters represent the ISO 3166-1 Alpha-2 country code. It is optional and often only used to distinguish between country-specific language dialects, like American English (en_US
) and British English (en_GB
). There's no such country like "England", so en_EN
is definitely invalid. You most likely want to use en
or perhaps en_US
here.
Thus, so
<default-locale>en</default-locale>
with an appropriate messages_en.properties
file.
How can i set my default language? The faces-config.xml default-locale does not seem to work.
The <default-locale>
is only used when the client didn't supply the locale by itself in flavor of the Accept-Language
HTTP request header, or supplied one which isn't supported by the application as definied in <supported-locale>
. A lot of (sane) clients actually set the Accept-Language
header. With a HTTP traffic monitor you can see them.
(the screen says: en-US
is the first preference (with a default "weight" of 1), the en
is the second preference with a "weight" of 0.8, the nl
is the third preference with a "weight" of 0.6; in Chrome you can change this in chrome://settings/languages
by adding/removing/reordering languages).
If you want to prevent this and want to force the "default" locale for some unclear reason, then you'd need to explicitly set the <f:view locale>
to the desired "default" locale (note: you're thus actually overriding client's preferred locale as configured in its program — the webbrowser).
E.g.:
<f:view locale="#{languageService.language}">
(just put in master template, wrapping the <h:head>
and <h:body>
)
with this new property in your backing bean:
@Named
@SessionScoped
public class LanguageService implements Serializable {
private Locale locale;
@PostConstruct
public void init() {
locale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
}
// ...
}
Also i need to know how i can change my localization.
Your mistake in this particular example is that you're not updating the view after invoking the action. You need to add update="@all"
to the command buttons. But there's another mistake: you're not remembering the locale for subsequent requests. It would fall back to the "default" locale again. You need to set the <f:view locale>
with this property. This is already answered in detail before: Localization in JSF, how to remember selected locale per session instead of per request/view.
这篇关于<默认语言环境>似乎不起作用,而是 <supported-locale>正在使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!