Upload
kai-jan
View
855
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
i18n and L10n in Node.js
Kai
livdea.com
• Resource file support• View template support• Language detection
but we are greedy….No using external resource, nor serviceRecently UpdateDocumentation
i18n-node• has all we need!• gets updated!!• is documented!!!!!!
我懂正體中文
{‘zh-tw’}
{‘我也說中文’ }
我也說中文
So…it’s like
Or….
mitä kuuluu
{‘fi’}
{‘How are you?’}
How are you?
Oops…..
That’s it. Let’s do it.
Build your ownhttps://github.com/kaijan/nodejs-taiwan-i18n.git
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});
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
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)
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)}
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);
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
Built on Node.js, MongodDB, Nginx and EC2