p:ajax for locale change one click behind problem

UI Components for JSF
Post Reply
User avatar
wikisky
Posts: 53
Joined: 16 Jul 2010, 17:30
Location: Oakville,ON
Contact:

21 Jul 2010, 05:47

I'm trying to partially reload page on language change. Everything works fine, except that it is one step behind. I mean when I change first language to second, the page reloaded in first language, then when I change back - the page now reloaded in second one. Here the code:

Code: Select all

<f:view contentType="text/html" locale="#{sessionBean.locale}">
...........
<h:outputText value="#{sessionBean.language}"/>
<h:selectOneMenu id="language_select" value="#{sessionBean.language}">
   <f:selectItem itemLabel="English" itemValue="en"/>
   <f:selectItem itemLabel="Français" itemValue="fr"/>
   <p:ajax process="language_select" update="sidebar1 post_block footer" event="change"/>
</h:selectOneMenu>

Code: Select all

    public Locale getLocale() {
        if (locale==null) {
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext ec=context.getExternalContext();
            locale=ec.getRequestLocale();
            if (locale==null) locale=Locale.ENGLISH;
        }
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

    public String getLanguage() {
        return getLocale().getLanguage();
    }

    public void setLanguage(String language) {
        setLocale(new Locale(language));
    }
Note, that I'm printing selected language at the beginning, and it does show that it changed in time, but all the renderings are still in previous language.
I tried to force locale "FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);" - doesn't work. I probably have to somehow reload bundles or something?

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

21 Jul 2010, 09:59

Hi,

Why do you change language in set-method? I would do it in valueChangeListener. This code works in my web app

Code: Select all

	/**
	 * This method is fired if the locale has been changed (see attribute valueChangeListener).
	 *
	 * @param vce ValueChangeEvent
	 */
	public void localeChanged(ValueChangeEvent vce)
	{
		Locale localeNew = (Locale) vce.getNewValue();

		FacesContext fc = FacesContext.getCurrentInstance();
		UIViewRoot viewRoot = fc.getViewRoot();

		if (viewRoot instanceof PartialViewRoot) {
			viewRoot = ((PartialViewRoot) viewRoot).getBase();
		}

		viewRoot.setLocale(localeNew);

		// force new loading of locales, themes and time zones because the language has been changed
		locales = null;
		themeNames = null;
		timeZones = null;
	}
The second strange thing is update="sidebar1 post_block footer". I would update the entire web application via redirect.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

User avatar
wikisky
Posts: 53
Joined: 16 Jul 2010, 17:30
Location: Oakville,ON
Contact:

21 Jul 2010, 22:58

Why do you change language in set-method? I would do it in valueChangeListener. This code works in my web app
Trust me I tried every method I could think of with my poor 2-weeks JSF experience. It didn't work. I just found the cause.
I was using in page

Code: Select all

<f:loadBundle basename="messages" var="msg"/>
. I found info that it is not recommended in JSF2 as it causing problems specifically in ajax calls. I had to set it in faces-config.xml:

Code: Select all

    <application>
        <resource-bundle>
            <base-name>messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
The second strange thing is update="sidebar1 post_block footer". I would update the entire web application via redirect.
I wanted to try cool looking in-page language switch.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

22 Jul 2010, 11:38

wikisky, so you resolved your issue?

User avatar
wikisky
Posts: 53
Joined: 16 Jul 2010, 17:30
Location: Oakville,ON
Contact:

22 Jul 2010, 13:43

Yes, the language does switch in page.
There are some issues with menu and submenu after I reload it by ajax - it stops highlighting and submenu doesn't pop up.
Didn't investigated it yet, but probably I will end up by reloading the whole page.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

22 Jul 2010, 14:28

As I remember Menu and MenuBar does not like being updated with ajax, we haven't investigated what this happens yet.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 45 guests