16
i18n and L10n in Node.js Kai livdea.com

I18n share

  • Upload
    kai-jan

  • View
    855

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: I18n   share

i18n and L10n in Node.js

Kai

livdea.com

Page 2: I18n   share
Page 3: I18n   share

• Resource file support• View template support• Language detection

Page 4: I18n   share

but we are greedy….No using external resource, nor serviceRecently UpdateDocumentation

Page 5: I18n   share

i18n-node• has all we need!• gets updated!!• is documented!!!!!!

Page 6: I18n   share

我懂正體中文

{‘zh-tw’}

{‘我也說中文’ }

我也說中文

So…it’s like

Page 7: I18n   share

Or….

mitä kuuluu

{‘fi’}

{‘How are you?’}

How are you?

Oops…..

Page 8: I18n   share

That’s it. Let’s do it.

Build your ownhttps://github.com/kaijan/nodejs-taiwan-i18n.git

Page 9: I18n   share

Installation and Configuration1. Installation

$: npm install i18n

2. Requirevar i18n = require("i18n");

3. app.configure()app.use(i18n.init); //language detection

4. i18n configurationi18n.configure({ locales:['zh-tw','zh'] // prefered language, and resource file});

Page 10: I18n   share

Installation and Configuration

5. For viewapp.locals({ __i: i18n.__ // so we can use i18n method in view});

6. Resource filei18n module will create a locales folder within the directory

where you execute your node server

Page 11: I18n   share

app.use(i18n.init)? Language detection

Loot at http request headersrequest['headers']['accept-language']zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4

would mean: "I prefer Chinese-Taiwan, but will accept United States English and other types of English." (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)

guessLanguage(request)

Page 12: I18n   share

Easy to code

Code in node.jsconsole.log(i18n.__('HelloWorld'));console.log(i18n.__('HelloGroup', 'Node.js', 30));

Code in view (using Jade)div #{__i('HelloWorld')}div #{__i('HelloGroup', 'node.js', 30)}

Page 13: I18n   share

Hi, TaiwanWe need zh-TW rather than zh(Chinese)in i18n.js, add the region detection

request.language_region = request.language ;

if (request.region){ request.language_region = request.language_region +'-'+ request.region;}

i18n.setLocale(request, request.language_region);

Page 14: I18n   share

Can’t switch language? Because defaultLocale is changed, but we can fix it

if (locales[target_locale]) { request.locale = target_locale; defaultLocale = target_locale; } else { defaultLocale = 'en'; request.locale = defaultLocale; }

Hi, Mr. Trouble

Page 15: I18n   share
Page 16: I18n   share

Built on Node.js, MongodDB, Nginx and EC2