Coder Social home page Coder Social logo

kraken_example_localization's Introduction

Kraken_Localization

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

What we'll be doing

Starting with a plain vanilla application, we're going to set up content bundles to greet the customer in different (random) languages!

Running this example

Clone this repo: git clone https://github.com/lensam69/Kraken_Example_Localization.git

Install the dependencies: npm install

Start the server: npm start

Relevant code

Logic

./controllers/index.js defines the / route. It populates a model with a name parameter, and calls the index template for rendering

Template

./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,

Content Bundle

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

Changing the code!

Adding more locales

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}!

See the changes here

Setting the locality

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>
        };

See the changes here

Seeing the changes in action

Restart your application, and visit http://localhost:8000/

Everytime you refresh the page, you will be greeted in a random language!

Is Klingon a thing?

According to Wikipedia there are 12 fluent speakers in the world. Isn't it nice that you can greet them?

kraken_example_localization's People

Contributors

jeffharrell avatar lensam69 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.