Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

jsf 2 - <default-locale> does not seem to work, instead the <supported-locale> is being used

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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.

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...