目次

Version 1, last updated by dpp at Nov 28 02:53 UTC

0.1 Localization

Lift has broad support for localization at the page and element level.

0.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.

0.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="hello">Ahoj</res>
  <res name="my_lang">Můj jazyk: </res>

  <res name="set_locale">Nastavit <b>Národní</b></res>
  <res name="change">Změnit</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="cs">Ahoj</res>
  <res name="hello" lang="en" default="true">Hello</res>
  <res name="hello" lang="en" country="US">Howdy, dude!</res>

  <res name="my_lang" lang="cs">Můj jazyk: </res>
  <res name="my_lang" lang="en" default="true">My Language: </res>
  <res name="set_locale" lang="cs">Nastavit Národní</res>
  <res name="set_locale" lang="en" default="true">Set Locale</res>

  <res name="change" lang="cs">Změnit</res>
  <res name="change" lang="en" default="true">Change</res>
</resources>

0.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”.

0.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.