3

In my application, on the Login page, there is a language option on top of the page.

Language option

If I choose Chinese, everything will be translated to Chinese perfectly.

Chinese

If I click Refresh button or Ctrl + R, the page is still in Chinese. However, if I open another tab going to the same URL, even though the language option still shows that Chinese was chosen, everything will be English again.

English again

Nevertheless, if you click About us or Methodology, the next page will be in Chinese again as it should be.

I'd be very grateful if you could tell me what I have done wrong here.

Best regards,

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Isn't the language code a part of your URL? Something like `www.example.com/en/help` or `www.example.com/cn/help`? – adarshr Jun 05 '12 at 09:30
  • @adarshr no, the language code is 1 of the properties in my `@SessionScoped` bean. – Mr.J4mes Jun 05 '12 at 09:32
  • I guess that might be the problem. You need to have something in the URL that identifies the language and rely on this code in the URL for switching the locale. Otherwise, bookmarks, shared URLs, URLs opened in new windows / browsers won't be localized. – adarshr Jun 05 '12 at 09:34
  • @adarshr hmmm... as I mentioned above, when I click on say `About us`, the next page is still in Chinese. It still can work rite? – Mr.J4mes Jun 05 '12 at 09:51

1 Answers1

2

There's apparently a problem in how you set the locale of the UIViewRoot.

It should be done by <f:view locale> as follows in your master template, perhaps you had this right for all other pages, but not for the home page.

E.g.

<!DOCTYPE html>
<html lang="#{localeBean.language}"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    ...
>
    <f:view locale="#{localeBean.locale}">
        <h:head>
            ...
        </h:head>
        <h:body>
            ...
        </h:body>
    </f:view>
</html>

Where #{localeBean} is a @SessionScoped one which look similar this.

This problem by the way indicates that the pages don't share a common master template and that you're duplicating XHTML code here and there. I'd work on that as well :)

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555