Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, at the risk of answering my own question, I will like to summarize all the different approaches that I have found.</p> <p>The basic approach is what I am already doing. That is, have a managed bean in session scope that returns the Locale of the user. This locale needs to be used in every XHTML using <code>&lt;f:view locale="..."&gt;</code>. I learned this technique from a post by BalusC, so thanks are due there.</p> <p>Now, the concern is the use of the f:view element. This needs to be repeated in every page, a potential source of defect if omitted by mistake. I have found a couple of ways of solving this problem.</p> <p>Approach #1: Create a Facelet template and add the f:view element there. Individual template user pages don't have to worry about adding this element.</p> <p>Approach #2 uses a phase listener. @meriton has posted the solution here. Thank you for that.</p> <p>Approach #3 uses a custom view handler that extends MultiViewHandler and returns user's locale from the calculateLocale() method. This is described in the book Beginning JSF 2 APIs and JBoss Seam By: Kent Ka Iok Tong. Here is a slightly altered example from the book:</p> <pre><code>public class MyViewHandler extends MultiViewHandler { public Locale calculateLocale(FacesContext context) { HttpSession session = (HttpSession) context.getExternalContext() .getSession(false); if (session != null) { //Return the locale saved by the managed bean earlier Locale locale = (Locale) session.getAttribute("locale"); if (locale != null) { return locale; } } return super.calculateLocale(context); } } </code></pre> <p>Then register it in faces config.</p> <pre><code>&lt;application&gt; &lt;view-handler&gt;com.package.MyViewHandler&lt;/view-handler&gt; &lt;!-- Other stuff ... --&gt; &lt;/application&gt; </code></pre> <p>This is somewhat more elegant than the phase listener. Unfortunately, MultiViewHandler is an internal non-API class from the com.sun.faces.application.view package. That incurs some risk going forward. </p> <p>With either approach #2 or #3, there is no need for the f:view element in the pages.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload