8.1 Localization
Lift has broad support for localization at the page and element level.
8.1.1 Localizing Templates
The locale for the current request is calculated based on the function in
LiftRules.localeCalculator. By default, the function looks at the
Locale in the HTTP request. But you can change this function to look at the Locale for the current user by changing
LiftRules.localeCalculator.
When a template is requested, Lift’s
TemplateFinder looks for a template with the suffix
_langCOUNTRY.html, then
_lang.html, then
.html. So, if you’re loading
/frog and your Locale is
enUS, then Lift will look for
/frog_enUS.html, then
/frog_en.html, then
/frog.html. But if your Locale is Czech, then Lift would look for
/frog_csCZ.html,
/frog_cs.html, and
/frog.html. The same lookup mechanism is used for templates accessed via the Surround (See
↓) and Embed (See
↓) snippets. So, at the template level, Lift offers very flexible templating.
Note: Lift parses all templates in UTF-8. Please make sure your text editor is set to UTF-8 encoding.
8.1.2 Resource Lookup
Lift uses the following mechanism to look up resources. Localized resources are stored in template files along-side your HTML pages. The same parser is used to load resources and the pages themselves. A global set of resources is searched for in the following files: /_resources.html, /templates-hidden/_resources.html, and /resources-hidden/_resources.html. Keep in mind that Lift will look for the _resources file using the suffixes based on the Locale.
The resource file should be in the following format:
<resources>
<res name="welcome">Benvenuto</res>
<res name="thank.you">Grazie</res>
<res name="locale">Località</res>
<res name="change">Cambia</res>
</resources>
In addition to global resource files, there are per-page resource files (based on the current Req.) If you are currently requesting page /foo/bar, the following resource files will also be consulted: /foo/_resources_bar.html, /templates-hidden/foo/_resources_bar.html, and /foo/resources-hidden/_resources_bar.html (and all Locale-specific suffixes.) You can choose to create a separate resource file for each locale, or lump multiple locales into the _resources_bar.html file itself using the following format:
<resources>
<res name="hello" lang="en" default="true">Hello</res>
<res name="hello" lang="en" country="US">Howdy, dude!</res>
<res name="hello" lang="it">Benvenuto</res>
<res name="thank.you" lang="en" default="true">Thank You</res>
<res name="thank.you" lang="it">Grazie</res>
<res name="locale" lang="en" default="true">Locale</res>
<res name="locale" lang="it">Località</res>
<res name="change" lang="en" default="true">Change</res>
<res name="change" lang="it">Cambia</res>
</resources>
8.1.3 Accessing Resources
Lift makes it easy to access resources.
From snippets: <span class="lift:Loc.hello">This Hello will be replaced if possible</span> Note that the value after the . in the snippet invocation is used to look up the resource name.
From code:
-
S.loc("hello") - return a Box[NodeSeq] containing the localized value for the resource named “hello”.
-
S.??("Hello World") - look for a resource named “Hello World” and return the String value for that resource. If the resource is not found, return “Hello World”.
8.1.4 Conclusion
Lift offers a broad range of mechanisms for localizing your application on a page-by-page and resource-by-resource by-resource basis.
(C) 2012 David Pollak