Problem with locale

UI Components for JSF
Post Reply
Simplicity
Posts: 5
Joined: 30 Oct 2010, 22:08

30 Oct 2010, 22:21

Hello everybody,

Excuse my poor english, I'm a french guy so I don't speak english very well ...

Ok ... I'm working under Netbeans 6.9.1, J2EE6, Mac OS X Snow Leopard, and tryin Primefaces 2.2-M1, the latest in download section from official website.

I have a problem when trying to switch locale with p:commandButton, ajax to false which reload my page.

I have some panels, two beans, and fr and en file properties.

The problem is that at the first time of submission, traduction is only applied where LocaleBean is present in XHTML. But if I click twice, that's ok, all msgs are translated ...

Do somebody know this issue ? It may/must come from me, but I don't undestand that behaviour. If somebody can help me ... Thanks.

index.xhtml

Code: Select all

        <f:view locale="#{localeBean.localeSelectedString}">
                <div style="width: 300px;">

                    <p:panel id="localePanel">
                        <f:facet name="header">
                            <h:outputText value="#{localeBean.localeSelectedString}" />
                        </f:facet>
                        <h:form id="localeForm">
                            <h:selectOneMenu id="localeFormCountry" value="#{localeBean.localeSelectedString}">
                                <f:selectItems value="#{localeBean.localeSelectItemList}"/>
                            </h:selectOneMenu>
                            <p:commandButton value="#{message.FORM_SUBMIT}" ajax="false" />
                        </h:form>
                    </p:panel>

                    <br />

                    <p:panel id="userPanel">
                        <f:facet name="header">
                            <h:outputText value="#{localeBean.localeSelectedString}" />
                        </f:facet>
                        <h:form id="userForm">
                            <h:panelGrid columns="2">
                                <h:outputLabel for="userFormLogin">#{message.USER_LOGIN} :</h:outputLabel>
                                <h:inputText id="userFormLogin" value="#{userBean.user.login}" />
                                <h:outputLabel for="userFormPassword">#{message.USER_PASSWORD} :</h:outputLabel>
                                <h:inputText id="userFormPassword" value="#{userBean.user.password}" />
                                <p:commandButton value="#{message.FORM_SUBMIT}" update="userInformationsLogin, userInformationsPassword" oncomplete="userInformations.show();" />
                                <p:ajaxStatus>
                                    <f:facet name="start">
                                        <h:graphicImage value="theme/picture/ajax-loader.gif" />
                                    </f:facet>
                                    <f:facet name="complete">
                                        <h:outputText value="" />
                                    </f:facet>
                                </p:ajaxStatus>
                            </h:panelGrid>
                        </h:form>
                    </p:panel>
                    <br />

                    <p:panel id="userListPanel">
                        <h:form id="userListForm">
                            #{localeBean.localeSelectedString}
                            <p:dataTable value="#{userBean.userList}" var="user" paginator="true" rows="10">
                                <p:column sortBy="#{user.userId}">
                                    <f:facet name="header">
                                        <h:outputText value="#{message.USER_ID}" />
                                    </f:facet>
                                    <h:outputText value="#{user.userId}" />
                                </p:column>
                                <p:column sortBy="#{user.login}" filterBy="#{user.login}" filterEvent="blur">
                                    <f:facet name="header">
                                        <h:outputText value="#{message.USER_LOGIN}" />
                                    </f:facet>
                                    <h:outputText value="#{user.login}" />
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="#{message.USER_PASSWORD}" />
                                    </f:facet>
                                    <h:outputText value="#{user.password}" />
                                </p:column>
                            </p:dataTable>
                        </h:form>
                    </p:panel>
                    <br />

                    <p:dialog header="Test de popup" widgetVar="userInformations" resizable="false" modal="true">
                        <h:panelGrid columns="2">
                            <h:outputText>#{message.USER_LOGIN} :</h:outputText>
                            <h:outputText id="userInformationsLogin" value="#{userBean.user.login}" />
                            <h:outputText>#{message.USER_PASSWORD} :</h:outputText>
                            <h:outputText id="userInformationsPassword" value="#{userBean.user.password}" />
                        </h:panelGrid>
                    </p:dialog>
                    <br />

                    <a href="#" onclick="userInformations.show();">Ouvrir</a>
                    <a href="#" onclick="userInformations.hide();">Fermer</a>

                </div>
        </f:view>
LocaleBean.java

Code: Select all

@Named(value = "localeBean")
@SessionScoped
public class LocaleBean implements Serializable {

    private HashMap<String, Locale> localeHashMap;
    private Locale localeSelected;
    private List<SelectItem> localeSelectItemList;
    private String localeSelectedString;

    /** Creates a new instance of LocaleBean */
    public LocaleBean() {
        localeHashMap = new HashMap<String, Locale>();
        localeSelected = null;
        localeSelectItemList = new ArrayList<SelectItem>();
        localeSelectedString = "";
    }

    @PostConstruct
    public void load() {
        loadLocaleList();
    }

    private void loadLocaleList() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Iterator<Locale> localeApplicationIterator = facesContext.getApplication().getSupportedLocales();
        while (localeApplicationIterator.hasNext()) {
            Locale localeApplication = localeApplicationIterator.next();
            localeHashMap.put(localeApplication.getLanguage(), localeApplication);
            localeSelectItemList.add(new SelectItem(localeApplication.getLanguage(), localeApplication.getDisplayName()));
        }
        localeSelectedString = facesContext.getApplication().getDefaultLocale().getLanguage();
    }

    public HashMap<String, Locale> getLocaleList() {
        return localeHashMap;
    }

    public void setLocaleList(HashMap<String, Locale> localeList) {
        this.localeHashMap = localeList;
    }

    public Locale getLocaleSelected() {
        localeSelected = localeHashMap.get(getLocaleSelectedString());
        return localeSelected;
    }

    public void setLocaleSelected(Locale localeSelected) {
        this.localeSelected = localeSelected;
    }

    public List<SelectItem> getLocaleSelectItemList() {
        return localeSelectItemList;
    }

    public void setLocaleSelectItemList(List<SelectItem> localeSelectItemList) {
        this.localeSelectItemList = localeSelectItemList;
    }

    public String getLocaleSelectedString() {
        return localeSelectedString;
    }

    public void setLocaleSelectedString(String localeSelectedString) {
        this.localeSelectedString = localeSelectedString;
    }
}
UserBean.java

Code: Select all

@Named(value = "userBean")
@SessionScoped
public class UserBean implements Serializable {

    @EJB
    private UserFacadeLocal userFacade;
    private User user;
    private List<User> userList;

    /** Creates a new instance of UserBean */
    public UserBean() {
    }

    public UserFacadeLocal getUserFacade() {
        return userFacade;
    }

    public void setUserFacade(UserFacadeLocal userFacade) {
        this.userFacade = userFacade;
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUserList() {
        if (userList == null) {
            userList = getUserFacade().findAllUsers();
        }

        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}
faces-config.xml

Code: Select all

<faces-config version="2.0"
    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">
    <application>
        <resource-bundle>
            <base-name>org.simplicity.core.message.language</base-name>
            <var>message</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
    </application>
</faces-config>

tkernstock
Posts: 65
Joined: 29 Jun 2010, 14:39
Location: Vienna, Austria

31 Oct 2010, 02:37

Hi,

I had the same problem (like so many others) - it took me hours of googeling to find a solution (posted by Marty Hall) -> you have to additionally execute the code FacesContext.getCurrentInstance().getViewRoot().setLocale(xxxxx) after changeing the local in your bean. For example:

I'm using icons instead of a dropdown ->

Code: Select all

	<p:commandLink action="#{localeHandler.setAktuelleLocaleDE}" ajax="false" >     
	     	<p:graphicImage value="./resources/images/flags/de.gif" />  
	</p:commandLink>

Code: Select all

public void setAktuelleLocaleDE() {
    	setAktuelleLocale("de");
   	FacesContext.getCurrentInstance().getViewRoot().setLocale(getAktuelleLokale());
}
I hope that helps.

regards
Thomas
Using: Eclipse 2020-06, Java 8, Primefaces 8, Omnifaces 3.6.1, Payara Server 5.201, Deltaspike 1.8.2

timotius_pamungkas
Posts: 117
Joined: 19 May 2010, 03:50

31 Oct 2010, 08:04

that should help, i've encountered same problem once and use same solution
Tomcat 7.0.25
Mojarra 2.1.6
Primefaces 3.1, 3.2
Firefox 10 and IE 7/IE 8

Simplicity
Posts: 5
Joined: 30 Oct 2010, 22:08

31 Oct 2010, 09:33

Many thanks to you. That works.

Here is the simplified LocaleBean.java code.

Greetings :)

LocaleBean.java

Code: Select all

@Named(value = "localeBean")
@SessionScoped
public class LocaleBean implements Serializable {

    private List<SelectItem> localeSelectItemList;
    private String localeSelectedString;

    /** Creates a new instance of LocaleBean */
    public LocaleBean() {
        localeSelectItemList = new ArrayList<SelectItem>();
        localeSelectedString = "";
    }

    @PostConstruct
    public void load() {
        loadLocaleHashMap();
    }

    private void loadLocaleHashMap() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Iterator<Locale> localeApplicationIterator = facesContext.getApplication().getSupportedLocales();
        while (localeApplicationIterator.hasNext()) {
            Locale localeApplication = localeApplicationIterator.next();
            localeSelectItemList.add(new SelectItem(localeApplication.getLanguage(), localeApplication.getDisplayName()));
        }
        localeSelectedString = facesContext.getApplication().getDefaultLocale().getLanguage();
    }

    public List<SelectItem> getLocaleSelectItemList() {
        return localeSelectItemList;
    }

    public void setLocaleSelectItemList(List<SelectItem> localeSelectItemList) {
        this.localeSelectItemList = localeSelectItemList;
    }

    public String getLocaleSelectedString() {
        return localeSelectedString;
    }

    public void setLocaleSelectedString(String localeSelectedString) {
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(localeSelectedString));
        this.localeSelectedString = localeSelectedString;
    }
}

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 20 guests