Search code examples
javatemplate-enginerythm

Rythm: create new engine or set Language/Locale on existing engine instance per request


Rythm can be used as a singleton or instantiated as a normal object. Currently I instantiate it once in a webapp in the Servlet Init() method. (This is almost the same as using a singleton since I'm only creating it once)

Now I need to add i18n support to my site and need to set the language dynamically before rendering.

I could do this:

engine.setLocale(userLocale);
engine.render(template,arguments);

I assume that if I use a singleton(or single instance created in the servlet) then I might get a race condition between the two statements.

  1. Is this correct or is there no race condition?
  2. Should I create a Rythm instance for each request?
  3. Is there another way to change the language for each rendering without changing the engine state?

Solution

  • First in the latest version of rythm, you should call engine.prepare(Locale), not engine.setLocale(Locale);

    Now to answer your questions:

    1. Is this correct or is there no race condition?

    It will NOT cause race condition because inside the code it uses ThreadLocal to store the render settings

    1. Should I create a Rythm instance for each request?

    Definitely NO

    1. Is there another way to change the language for each rendering without changing the engine state?

    Language and locale are NOT engine level states, they are per request states. However default language and locale could be configured at engine level