2

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?

Community
  • 1
  • 1
R.Almoued
  • 219
  • 6
  • 16
  • I presume that #{locale} does not exist? Then you may want to use the backing-beans `locale`(it is `java.util.Locale` I presume?) property instead instead: `#{localeManager.locale}`. But I'm not 100% sure that XMLs are allowed to contain EL code ... – Roland Jul 04 '18 at 22:28
  • **OT**: TIL that `Manager` is following a non-strict naming-convention: https://stackoverflow.com/questions/5697351/jsf-managed-bean-naming-conventions because I was about to write that `Manager` is not proper named. So all okay here. However, being **off-topic** now, maybe you should consider upgrading your out-dated annotations `@ManagedBean` and `@*Scoped` to newer `@Named` (import `javax.inject.Named`) and `@*Scoped` from `javax.enterprise.context` package. – Roland Jul 04 '18 at 22:32

1 Answers1

2

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.

Lincoln
  • 3,151
  • 17
  • 22
  • thank for your answer. I just confused about the parameter to use. So I have questions. 1- don't we need any setting to be added as separate xml file? for prettyfaces we have pretty-config.xml, do we need one for rewrite?? – R.Almoued Aug 04 '16 at 12:58
  • 2- I still confused about the parameters in the code you gave. I want to use the last one with "Join". Now my home page is http://localhost:8080/Wirote/index.xhtml and my resources bundle location is: src/main/resources/I18n/Paths_en.properties I have te following in the Code: .addRule(Join.path("/{lang}/{path}").to("/{path}.jsp")). The question is: what should be "path" in my case? will it works only for one page or for the whole web site?? – R.Almoued Aug 04 '16 at 12:58
  • 1
    Check out http://ocpsoft.org/rewrite/ --- Your rule will apply to the entire site (depending on how you construct it.) Rewrite no longer requires XML configuration of any kind. `{path}` in this case defines a parameter that is used as a pattern to match at that location in the URL. Just like PrettyFaces used `#{path}` to define URL parameters ... it is the same thing. – Lincoln Aug 04 '16 at 20:18
  • please check my configuration above – R.Almoued Aug 05 '16 at 06:48