R.Almoued
R.Almoued

Reputation: 479

Multi-language url rewriting using Prettyfaces

I built my site to be Multi-language. I want the language code to be embedded in the address of page according to the locale. I have the following:

http://localhost:8080/Wirote/index

I want to have it as the following:

http://localhost:8080/Wirote/de/index --- display German content
http://localhost:8080/Wirote/en/index --- display English content
http://localhost:8080/Wirote/ar/index --- display Arabic content

To achieve this I followed the step in : multi-language url rewiting. Is it possible?

pretty-config.xml

<url-mapping id="base">    
    <pattern value="/#{localeManger.language}"/> 
 </url-mapping>
 
 <url-mapping id="index" parentId="base">
    <pattern value="/index"/>
    <view-id value="/index.xhtml"/> 
 </url-mapping>

faces-config.xml

<application>
        <locale-config>
            <default-locale>de</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>ar</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>I18n.lang</base-name>
            <var>sprache</var>
        </resource-bundle>
    </application>

LocaleManger.java

@ManagedBean(name = "localeManger")
@SessionScoped
public class LocaleManger implements Serializable{

    private Locale locale;
    private static final long serialVersionUID = 2756934361134603857L;

    @PostConstruct
    public void init() {
 FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
    }

    public Locale getLocale() {
        return locale;
    }

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

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

Now when I run the project, I got only:

http://localhost:8080/Wirote/index

also the page is stuck, so I can't navigate to another using links in the index.xtml

alternative I add the following to the index.xhtml:

  <f:metadata>
        <f:viewParam name="locale" value="#{localeManger.language}"/>
  </f:metadata>

pretty-config.xml

 <url-mapping id="index">
    <pattern value="/#{locale}/index"/>
    <view-id value="/index.xhtml"/> 
 </url-mapping>

Now when I run the project, I get the following:

http://localhost:8080/Wirote/de/index

But when I try to change the language, by clicking on English or Arabic language switcher, it doesn't work correctly, the content of the page change, but the address page is not. But if I change it manually to

http://localhost:8080/Wirote/en/index   or
http://localhost:8080/Wirote/ar/index

it display the correct content in Arabic and English, but I need the address to be changed automatically not manually.

How can I get the correct address related to current locale?

Upvotes: 2

Views: 581

Answers (1)

Lincoln
Lincoln

Reputation: 3191

I'm not sure if this helps you, since you're attempting to do this using PrettyFaces, but Rewrite (the library PrettyFaces is based on) has a dedicated feature for internationalizing URLs:

https://github.com/ocpsoft/rewrite/blob/master/documentation/src/main/asciidoc/configuration/i18n.asciidoc

This might help you and if you are using PrettyFaces based on Rewrite, then you already have access to this feature.

Upvotes: 2

Related Questions