Adding language code in the page adress – Multi-language url rewiting

Splash Forums PrettyFaces Users Adding language code in the page adress – Multi-language url rewiting

This topic contains 2 replies, has 2 voices, and was last updated by  vector_ever 59 minutes ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #27435

    vector_ever
    Participant

    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 // even I used Requestscoped instead
    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?

    #27446

    Hi There,

    I believe your issue is related to the fact that you are likely using the JSF Page Navigation system — You need to issue a page redirect in order to change the URL after navigation using the language picker you’ve implemented.

    This page has a good explanation of the difference between Forward (the default) and Redirect (what you need).

    https://www.mkyong.com/jsf2/jsf-page-forward-vs-page-redirect/

    Hope this helps.

    Additionally, consider using Rewrite (the library PrettyFaces is built on) for internationalization:

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

    Thanks,
    Lincoln

    #27450

    vector_ever
    Participant

    Your solution not clear enough to me, also it describes another case.

    Again I am not sure if I did it correctly When I run my project I still get : http://localhost:8080/Wirote/index.xhtml

    My properties (lang.properties) files in the following location:

    src/main/resources/I18n/lang.properties

    But in the example they have another location:

    src/main/java/org/example/Paths_en.properties ( it is in Java instead of resources)

    I mean instead of resource folder you have it in java package!

    The most importan property in the properties file is “language” which is resolve to language code

        src/main/resources/I18n/lang.properties .... language = en
        src/main/resources/I18n/lang_en.properties .... language = en
        src/main/resources/I18n/lang_de.properties .... language = de
        src/main/resources/I18n/lang_ar.properties .... language = ar

    according to this I change the code to be:

    public class LocalizedConfig extends HttpConfigurationProvider{
       @Override
       public Configuration getConfiguration(final ServletContext context)
       {
          return ConfigurationBuilder.begin()
                   .addRule(Join.path("/{language}/{path}").to("/{path}.xhtml"))
                   .where("path").transposedBy(LocaleTransposition.bundle("I18n.lang", "language"));
       }
        @Override
        public int priority() {
            throw new UnsupportedOperationException("Not supported yet."); 
        }}

    I know something is wrong but I don’t know what it is.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.