< default-locale>似乎不起作用,而是< supported-locale>正在被使用 [英] <default-locale> does not seem to work, instead the <supported-locale> is being used

查看:138
本文介绍了< default-locale>似乎不起作用,而是< supported-locale>正在被使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的jsf网站项目的默认位置设置为英语,所以 我做了以下事情:

i wanted to set the default location of my jsf web project to english, so i did the following:

faces-config.xml

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>

我的src文件夹de.hof.tschuwwa.resources中有一个包 该程序包中有两个属性文件:

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

我在一个小的xhtml文件中对其进行了测试:

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.

如何设置默认语言? faces-config.xml默认语言环境 似乎不起作用.

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>

首先,您在语言环境标识符en_EN中犯了一个错误.前两个小写字符表示 ISO 693-1 语言代码.它是可选的,并且仅在显示国家或地区代码时才需要.最后两个大写字符代表 ISO 3166-1 Alpha-2 国家/地区代码.它是可选的,通常仅用于区分特定国家/地区的语言方言,例如美国英语(en_US)和英式英语(en_GB).没有像英格兰"这样的国家,所以en_EN绝对无效.您最有可能在这里使用enen_US.

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.

因此

<default-locale>en</default-locale>

带有适当的messages_en.properties文件.

如何设置默认语言? faces-config.xml默认语言环境似乎不起作用.

仅当客户端自身不提供Accept-Language HTTP请求标头的形式的语言环境,或提供了<supported-locale>中定义的应用程序不支持的语言环境时,才使用<default-locale>.许多(理智的)客户端实际上都设置了Accept-Language标头.使用HTTP流量监控器,您可以看到它们.

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.

(屏幕上显示:en-US是第一个首选项(默认权重"为1),en是第二个首选项,权重"为0.8,nl为第三个首选项的权重"为0.6;在Chrome中,您可以在chrome://settings/languages中通过添加/删除/重新排序语言来更改此设置).

(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).

如果您要防止这种情况并出于某些不清楚的原因而强制使用默认"语言环境,则需要将<f:view locale>显式设置为所需的默认"语言环境(请注意:实际上,您实际上是覆盖客户端的首选语言环境(如其程序&webdash; webbrowser中所配置).

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).

例如:

<f:view locale="#{languageService.language}">

(只需放在主模板中,将<h:head><h:body>包装)

(just put in master template, wrapping the <h:head> and <h:body>)

在您的后备bean中具有此新属性:

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();
    }

    // ...
}


我还需要知道如何更改本地化.

您在此特定示例中的错误是,您在调用操作后没有更新视图.您需要将update="@all"添加到命令按钮.但是还有另一个错误:您不记得后续请求的语言环境.它将再次回到默认"区域设置.您需要使用此属性设置<f:view locale>.之前已经对此进行了详细解答: JSF中的本地化,如何记住每个会话而不是每个请求/视图选择的语言环境.

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.

这篇关于&lt; default-locale&gt;似乎不起作用,而是&lt; supported-locale&gt;正在被使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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