使用commandLink更改语言

时间:2012-07-22 12:46:38

标签: jsf internationalization primefaces

我正在尝试使用p:commandLink更改语言。

我有一个模板(template.xhtml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:pm="http://primefaces.org/mobile"
    xmlns:p="http://primefaces.org/ui">

<f:loadBundle
    basename="labels.ClientLabels"
    var="labels" />

<h:head>
    <title><ui:insert name="windowTitle" /></title>
</h:head>

<h:body>
    <f:view locale="#{localeChanger.locale}">
        <pm:page title="aaaa" swatch="a">
            <pm:view id="main">
                <!-- HEADER -->
                <ui:insert name="header">
                    <!-- the default header is used if no replacement for header is specified when the template is used -->
                    <pm:header fixed="true" title="Header" swatch="a">

                            <f:facet name="left">
                                <p:commandLink action="#{localeChanger.setEnglish}" onchange="submit()">
                                    <h:graphicImage value="image/uk.png" />
                                </p:commandLink>
                            </f:facet>

                            <f:facet name="right">
                                <p:commandLink action="#{localeChanger.setGerman}" onchange="submit()">
                                    <h:graphicImage value="image/de.png" />
                                </p:commandLink>
                            </f:facet>  
                    </pm:header>
                </ui:insert>

                <!-- CONTENT -->
                <ui:insert name="content">
                    <!-- default content -->
                    <pm:content>
                        <h:form></h:form>
                    </pm:content>
                </ui:insert>

                <!-- FOOTER -->
                <ui:insert name="footer">
                    <!-- the default footer -->
                    <pm:footer fixed="true" swatch="a">
                        <pm:navBar>

                            <p:button outcome="info" value="#{labels.Info}" icon="info" />      
                                                    <p:button outcome="userlogin" value="#{labels.User}" icon="star" />
                        </pm:navBar>
                    </pm:footer>
                </ui:insert>
            </pm:view>
        </pm:page>
    </f:view>
</h:body>
</html>

userlogin.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:pm="http://primefaces.org/mobile"
    xmlns:p="http://primefaces.org/ui">

<head></head>

<body>
    <ui:composition template="template.xhtml">
        <ui:define name="content">
            <pm:content>
                <h:outputText value="#{labels.IntroductionText}" />
                <h:form>
                    <h:panelGrid columns="2" cellpadding="5">
                        <h:outputLabel for="username" value="#{labels.UserName}" />
                        <p:inputText value="#{login.username}" id="username"
                            required="true" />

                        <h:outputLabel for="password" value="#{labels.Password}" />
                        <h:inputSecret value="#{login.password}" id="password"
                            required="true" label="password" />

                        <f:facet name="footer"> 
                        <p:commandButton id="loginButton" value="#{labels.Login}" />
                        </f:facet>
                    </h:panelGrid>
                </h:form>
            </pm:content>
        </ui:define>
    </ui:composition>
</body>
</html>

LocaleChanger.java:

@ManagedBean
@SessionScoped
public class LocaleChanger implements Serializable{

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    public Locale getLocale() {
        return locale;
    }

    public String getLanguage() {
        System.out.println(locale.getLanguage());
        return locale.getLanguage();
    }

    public void setLanguage(String language) {
        locale = new Locale(language);
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

    public void setEnglish(){
        System.out.println("english");
        setLanguage("en");
    }

    public void setGerman(){
        System.out.println("german");
        setLanguage("de");
    }
}

faces-config.xml:

<managed-bean>
        <managed-bean-name>localeChanger</managed-bean-name>
        <managed-bean-class>test.LocaleChanger</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>de</supported-locale> 
        </locale-config>
        <default-render-kit-id>PRIMEFACES_MOBILE</default-render-kit-id>
    </application>

当我点击commandLinks时没有任何反应。调用了两个方法setEnglish和setGerman,但标签没有变化。

我的属性文件是: ClientLabels_en.properties和 ClientLabels_de.properties

我正在使用嵌入式Jetty,JSF 2.0,PrimeFaces 3.3和PrimeFaces Mobile 0.9.3。 非常感谢你。

更新

文件ClientLabels_en.properties:

Info        =       Info
User        =       User

IntroductionText    =   Please enter your username and password 
UserName            =   Username:
Password            =   Password:
Login               =   Login

文件ClientLabels_de.properties是相同的,但是有值的德文文本。

我还在ClientLabels_de_DE.properties中更改了文件的名称,但问题仍然存在。

0 个答案:

没有答案