A sample application to show the localization features of Kraken If you'd like to see a non-trivial application, check out a shopping cart built on kraken
Starting with a plain vanilla application, we're going to set up content bundles to greet the customer in different (random) languages!
Clone this repo: git clone https://github.com/lensam69/Kraken_Example_Localization.git
Install the dependencies: npm install
Start the server: npm start
./controllers/index.js
defines the /
route. It populates a model with a name
parameter, and calls the index template for rendering
./public/templates/index.dust
Is the template that greets the user.
Within it, we have the following entry:
<h1>{@pre type="content" key="index.greeting"/}</h1>
This dust tag, tells the framework that it should be populated from the index.greeting key in the index bundles,
./locales/
is where the content bundles are located. Initially we only have one: /locales/US/en/index.properties
.
As you can see, content bundles are nothing more than properties files, stored according to locality (In this case, /US/en/)
This content bundle only has one entry:
index.greeting=Hello, {name}!
Which will be used by the template to greet the user.
Let's add a few more languages (Spanish, French Canadian and Klingon) so that we can greet the user. To add a locale,
you need to create the appropriate folders in ./locales/
, by country and language. (Even if fictional). Next, we'll add
the content bundle for each locale. This is nothing more than creating index.properties
files for each, with the right content:
Locale | Folder to create | index.properties |
---|---|---|
Spanish (es_ES) | /ES/es | index.greeting=Hola, {name}! |
French Canadian (fr_CA) | /CA/fr | index.greeting=Bonjour, {name}! |
Klingon (tlh_US) | /US/tlh | index.greeting=nuqneH, {name}! |
Finally, we need to let kraken know what language we want to use.
This is done by adding a context to the response. This context will include the locality we want to use. We'll make this change inline
on the routing logic ./controllers/index.js
before invoking the template
res.locals.context = {
locality: <locale_to_use>
};
Restart your application, and visit http://localhost:8000/
Everytime you refresh the page, you will be greeted in a random language!
According to Wikipedia there are 12 fluent speakers in the world. Isn't it nice that you can greet them?