title: gettext-commons per-request locale in servlet
body: <p>The <a href="https://code.google.com/p/gettext-commons/wiki/Tutorial" rel="nofollow">gettext-tutorial</a> states that </p>
  
  <blockquote>
    <p>The preferable way to create an I18n object is through the I18nFactory. The factory caches the I18n object internally using the the package name of the provided class object and registers it with I18nManager. Thus all classes of the same package will use the same I18n instance.</p>
  </blockquote>
  
  <p>But running inside an HttpServlet (actually an VelocityViewServlet in my particular case) it seems to me that if I change the locale on the cached object it could stay like that for subsequent requests and that is not desirable because each request must be served with particular locales. </p>
  
  <p>So the question is, should I instead create ad-hoc i18n objects on a per-request basis, with the specific locale of that request? I think that would be a performance penalty, so should I instead create my own manager and select the appropriate instance based on the request's locale?</p>
  
  <p>Any comments greatly appreciated. There doesn't seem to be much doc or examples besides the tutorial and the Javadoc.</p>
  
  <p>Update: There is one thing I tried and seems to work. Instead of declaring a private static member like the tutorial says, I just call the factory method on each request, using this special version of the I18nFactory:</p>
  
  <pre><code>getI18n(java.lang.Class clazz, java.lang.String bundleName, java.util.Locale locale)
  </code></pre>
  
  <p>This should get a cached instance of that particular locale (or force the load on the first request for that locale), avoiding the performance penalties on subsequent requests for the same one. If anyone can confirm this make sense:</p>
  
  <pre><code>public class Foo extends VelocityViewServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
      Locale reqLocale = request.getLocale();
      I18n i18n = I18nFactory.getI18n(Foo.class, "app.i18n.Messages", reqLocale);
      String localized = i18n.tr("Bar"); 
      ...
    }
  ...
  }
  </code></pre>
  
  <p>Again, I've tested this and it's working but I'd like to know if it's a correct implementation of gettext-commons for this type of application.</p>
  
  <p>Thanks in advance,</p>
  
  <p>-- 
  Alejandro Imass</p>