Reply To: Problem with changing language while using language base in SEO urls

Splash Forums PrettyFaces Users Problem with changing language while using language base in SEO urls Reply To: Problem with changing language while using language base in SEO urls

#24306

Hey, let’s see if I can help.

First of all: the two pretty-config.xml variants you posted are equivalent, with one minor difference. If you specify a parent mapping, PrettyFaces will concatenate both patterns to a single one. In your example this leads to /#{ lang : localeChanger.language }//login. The problem here is the //. So I guess you should remove the trailing slash from the parent mapping to get the expected result.

Regarding your LocaleChangerBean I have a few comments:

(1) It’s weird that this bean is view scoped. As it manages the language parameter which is part of the current URL, it should be request scoped.

(2) The URL in your address bar will only change if you send a redirect from your action method (which is the “else” part of your code). So returning null doesn’t make any sense because the URL will be the same as before.

(3) If your bean returns something like /someview.xhtml?faces-redirect=true, you will also have to set the language to the desired value for the outbound URL rewriting to work correctly. Something like /someview.xhtml?lang=en&faces-redirect=true.

(4) I think it won’t work very good if you just change the language in the view root. Instead you should use f:view and use the locale attribute to specify the locale you want to use for the view. You can simply reference your bean for that. See the example below.

(5) To send the correct redirect, it would be easier to return something like pretty:someId. To redirect the current mapping you can use:

return "pretty:"+PrettyContext.getCurrentInstance().getCurrentMapping().getId();

So your class could look like this:

@ManagedBean(name = "localeChanger")
@RequestScoped
public class LocaleChangerBean implements Serializable {

    private String language = "en";   // the default language

    public String englishAction() {

        /* set the language to the new value */
        this.language = "en";

        /* either this way */
        return "pretty:" + PrettyContext.getCurrentInstance().getCurrentMapping().getId();

        /* or this way */
        return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?lang=" + language + 
                    "&faces-redirect=true";

    }

    public Locale getLocale() {
        return new Locale(language);
    }

    ....
}

And use this in your view:

<f:view locale="#{localeChanger.locale}">
  ....
</f:view>

This way JSF will _always_ use the language that your LocaleChangerBean has. So you don’t need to change the locale in the view any more. Just change the property in your bean and redirect, so that the URL changes.

I hope this helps a bit. I think there are many ways to implement such a requirement. And that’s the way I would implement it. 🙂