OK, at the risk of answering my own question, I would like to summarize all the different approaches that I have found.
The basic approach is what I'm already doing. That is, to have a managed bean in the session area that returns the user's Locale. This language should be used in every XHTML using <f:view locale="..."> . I learned this technique from a BalusC post, so thanks there.
Now the problem is using the f: view element. This must be repeated on each page, the potential source of the defect, if mistakenly mistaken. I have found several ways to solve this problem.
Approach No. 1: create a Facelet template and add the f: view element there. Separate pages of custom templates should not worry about adding this element.
Approach No. 2 uses a phase listener. @meriton posted the solution here. Thanks for this.
Approach No. 3 uses a specialized view handler that extends MultiViewHandler and returns a custom language from the calculateLocale () method. This is described in The Beginning of the JSF 2 and JBoss Seam APIs: Kent Ka Yok Tong. Here is a slightly modified example from the book:
public class MyViewHandler extends MultiViewHandler { public Locale calculateLocale(FacesContext context) { HttpSession session = (HttpSession) context.getExternalContext() .getSession(false); if (session != null) {
Then register it in the face configurations.
<application> <view-handler>com.package.MyViewHandler</view-handler> </application>
It is somewhat more elegant than a phase listener. Unfortunately, MultiViewHandler is an internal non-API class from com.sun.faces.application.view package. This leads to some risk in the future.
With either approach # 2 or # 3, there is no need for an f: view element on the pages.
source share