How to localize

From Lift

Jump to: navigation, search

Contents

Overview

THIS PAGE IS UNDER CONSTRUCTION - TESTED WITH lift 04-SNAPSHOT version.

Lift has full support for internationalization and localization of text in code and templates.

Based on the current locale lift can selects the appropriate template. You can also localize a subsection of a default template or in inline code. Here is how it works.

Template Subsection Localization

Let's start with a simple example. We assume that both the browser and the site default languages are English. You have the following project structure.

...
+ webapp
++++ pages
++++++++ hello.html
...

The content of hello.html is:

<lift:surround with="default" at="content">
    <h1>Hello</h1>
    I speak English
</lift:surround>

When browsing at http://localhost:8080/pages/hello you can see something like:

Hello
I speak English

Now, since freedom fries are becoming somewhat less fashionable, you feel compelled to serve your page also in French, depending on the current locale. You change your hello.html template to:

<lift:surround with="default" at="content">
    <h1><lift:loc>Hello</lift:loc></h1>
    <lift:loc locid="speak">I speak English</lift:loc>
</lift:surround>

The <lift:loc/> will retrieve and render a localized message from a message store based on the current locale and a key.

The key is either the locid attribute, if present, or the <lift:loc/> element content. In the example above, "Hello" is the key in line 2 and "speak" is the key in line 3.

The <lift:loc/> element content is also the default message when a key is not found in the store. If you refresh the page in the browser, you won't see any change.

To see the localized messages in French, you need to do two more thinks:

  1. Tell lift to select a French locale. There are several ways to do that, but for now we'll try the simplest one. Select a French locale as your preferred language (for example French/Canada)
  2. Populate your store with French messages. We will use files properties as the message store. Let's create a file at ${yourProjectBaseDir}/src/main/resources/lift_fr.properties.
 Hello=Bonjour
 speak=Je parle français

The file must be in the classpath when your servlet container starts for lift to use it to retrieve the messages. If you use the jetty plugin, you can simply go to ${yourProjectBaseDir} from a console and run mvn resources:resources and let the jetty plugin detect your changes and automatically restart the container (assuming the plugin is properly configured).

When refreshing your browser you can now see something like:

Bonjour
Je parle français

Message Stores

Resource Bundles

Using a Localizer

See idea of Viktor Klang.

  • database
  • resource bundle (does this will deprecate the current use of resource bundles or will it be reimplemented with the Localizer?)

Inline Code Localization

Sometimes you need your message can depends on some computation. That's where you can use inline code localization.

Let's create a snippet:

package org.myapp.snippet

import java.util.Random
import net.liftweb.http.S.?

class Hello {
  private val n = new Random().nextInt(100)
  def whoami = <span>{if (n % 2 == 0) ?("even") else ?("odd")}</span>
}

The important method here is S.?.

The ? method will lookup a localized message with the key "even" if the generated number is even or with the key "odd" otherwise. Don't forget to add this custom snippet package as one being looking at by lift when it's searching for a snippet (ie: LiftServlet.addToPackages("org.myapp.snippet") int the Boot.boot method)

Add your messages to lift_fr.properties:

 Hello=Bonjour
 speak=Je parle français
 even=Je suis un nombre pair
 odd=Je suis un nombre impair

Add your Hello:whoami snippet your hello.html template accordingly:

<lift:surround with="default" at="content">
    <h1><lift:loc>Hello</lift:loc></h1>
    <lift:loc locid="speak">I speak English</lift:loc>
    <lift:snippet type="Hello:whoami"/>
</lift:surround>

From your console run mvn resources:resources. After hitting the refresh button on your browser you will see something like:

Bonjour
Je parle français
Je suis un nombre pair

I18N

Template Based Localization

Message Parsing

TODO: example of a message
Hello <em>Roland</em> Dumas

Media Localization

Missing Keys

Locale Selection

How lift selects the current locale.

Changing the Current Locale

Output Content Type and Charset

Translations and Javascript

Personal tools