- Lift Application Development Cookbook
- Gilberto T. Garcia Jr.
- 910字
- 2021-08-04 10:05:45
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
- 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
- The
- 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!
- 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!
- 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...
- 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
- 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. - Replace the content of the
div
tag withmain
as the value of theid
field in theindex.html
file to:<h2 data-lift="Loc.i">welcome.text</h2> <p><span class="lift:Localization.dynamic"></span></p>
- 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") } }
- 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: - If you change your browser's language to German, you should see the page loaded in German, as shown in the following screenshot:
- 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
See also
- You can learn more about template localization at:
- In addition, to learn more about resource bundles, visit: http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader)
- Learning Java Functional Programming
- Computer Vision for the Web
- 圖解Java數據結構與算法(微課視頻版)
- 我的第一本算法書
- 從0到1:HTML+CSS快速上手
- Mastering Swift 2
- C++ 從入門到項目實踐(超值版)
- Python機器學習算法與實戰
- Teaching with Google Classroom
- PySpark Cookbook
- Test-Driven Machine Learning
- GameMaker Essentials
- Unity 2018 Augmented Reality Projects
- Django 5企業級Web應用開發實戰(視頻教學版)
- TypeScript圖形渲染實戰:2D架構設計與實現