官术网_书友最值得收藏!

Localizing templates

Sometimes, applications or websites we build need to support multiple languages. Lift supports this requirement by allowing us to localize templates, and offers a powerful yet simple way to choose which template to use when rendering the page.

In this recipe, we'll learn how to localize templates and texts that will be loaded dynamically for our applications or websites.

Getting ready

  1. Create a new project and the following three bundle files in the src/main/resources/i18n folder:
    • The resources.properties file
    • The resources_de.properties file
    • The resources_fr_FR.properties file
  2. The resources.properties file will have the following content:
    menu.home=Home
    menu.static=Static
    dynamic.text=Some dynamic text
    welcome.text= Welcome to your project!
  3. While the file resources_de.properties will have the content shown in the following code:
    menu.home=Zuhause
    menu.static=Statisch
    dynamic.text=einige dynamischen Text
    welcome.text= Willkommen in Ihrem Projekt!
  4. And the file resources_fr_FR.properties will have the content shown in the following code:
    menu.home=Page d'accueil
    menu.static=Statique
    dynamic.text=Un texte dynamique
    welcome.text= Bienvenue à votre projet!

How to do it...

  1. We first need to tell Lift where it can find the files with the texts in different languages, also known as bundle files. You can do this by adding the following code in the Boot class:
    LiftRules.resourceNames = "i18n/resources" :: 
      LiftRules.resourceNames
  2. Consider the following menu in the Boot.scala file:
    Menu.i("Home") ….
    …. "Static Content")))

    You will need to change it to:

    Menu("menu.home") ….
    …. S ? "menu.static")))

    We are also going to change the index.html file so that Lift can localize the static text.

  3. Replace the content of the div tag with main as the value of the id field in the index.html file to:
    <h2 data-lift="Loc.i">welcome.text</h2>
    
    <p><span class="lift:Localization.dynamic"></span></p>
  4. At last, we will need to create a file called Localization.scala in the snippet package to dynamically load localized text, as shown in the following code:
    import net.liftweb.util.BindHelpers._
    import net.liftweb.http.S
    
    class Localization {
      def dynamic = {
        "*" #> S.?("dynamic.texttext")
      }
    }
  5. Start the application and access http://localhost:8080. You should see the page loaded in English, assuming English is the default language set in your browser, as shown in the following screenshot:
  6. If you change your browser's language to German, you should see the page loaded in German, as shown in the following screenshot:
  7. Finally, if you change your browser's language to French, you should see the page loaded in French, as shown in the following screenshot:

How it works...

We localized our application or website by doing two things. We first localized our templates so that we can have the static content translated into the correct language. We achieve this by using the built-in snippet called Loc. This snippet uses the string inside the tag—welcome.text, in our case—as a key and tries to find a resource in the bundle files defined by that key.

Lift has a built-in locale calculator that will try to find the correct bundle based on the language set in your browser, which is passed to the server via a parameter in the HTTP header request. For example, if the language set in your browser is German, Lift will try to find a resources_de.properties file. Three things can happen here:

  • Lift finds the bundle file and renders the template in the correct language
  • Lift doesn't find the file and then tries to find the default bundle—resources.properties in this case—and renders the text in the language set in the default bundle
  • Lift doesn't find any bundles and then renders the template with welcome.text as the key—in our case

The same mechanism is applied to our Localization snippet and MenuBuilder snippet. When you use S ? ("key"), you are invoking the ? method in the S object. This method will try to get the value defined by the key in a property file. The trick here is that the ? method loads a localized string, which means that it will try to find a resource bundle file that best fits the locale defined in the request. How does Lift know which resource bundle file to use and where to find it?

Lift knows where to find the resource bundle file because we have already told it where they are when we added LiftRules.resourceNames in the Boot class, and it knows which file to use, because it uses the same resolution system used to find HTML templates—locale ISO codes.

In short, by calling the ? method from the S object and using the built-in snippet called Loc, we localized the menu and the text loaded dynamically using resource bundles.

Note

This will not work when using localization with Comet actors because parameters in the HTTP request will be empty. In this case, it's better to keep localization information in SessionVars.

There's more...

The resolutions of both templates and bundle files follow the same logic. This means that Lift will try to find the template and bundle files starting in the most specific locale and moving toward the most generic one, as shown in the following example:

8. baseName + "_" + language1 + "_" + country1 + "_" + variant1

9. baseName + "_" + language1 + "_" + country1

10. baseName + "_" + language1

11. baseName + "_" + language2 + "_" + country2 + "_" + variant2

12. baseName + "_" + language2 + "_" + country2

13. baseName + "_" + language2

14. baseName

主站蜘蛛池模板: 六安市| 措美县| 游戏| 红原县| 柳州市| 姜堰市| 剑川县| 南京市| 驻马店市| 玛多县| 新巴尔虎右旗| 化德县| 临颍县| 凉山| 滦平县| 句容市| 新巴尔虎左旗| 安丘市| 中卫市| 屏东市| 平谷区| 耿马| 阿坝县| 淅川县| 淮南市| 永春县| 石楼县| 沛县| 肥城市| 呼玛县| 高尔夫| 宝鸡市| 滁州市| 保靖县| 开鲁县| 红原县| 白山市| 营山县| 安徽省| 山阳县| 三明市|