lengua
v0.4.1
Published
Fork from Lingua by André König. Added express support and language fallbacks.
Downloads
4
Readme
Lingua
Lingua is a middleware for the Express.js framework that helps you to internationalise your webapp easily. It determines the language of the user agent and pushes the i18n resources to your views.
Installation
$ npm install lingua
Quick Start
Using lingua comes down with four simple steps:
Grab lingua
var express = require('express'), lingua = require('lingua'); ... // Express init code goes here ... // Express app configuration code and lingua init. app.configure(function(){ ... app.register(".html", require("jqtpl").express); app.set('views', __dirname + '/views'); app.set("view engine", "html"); // Lingua configuration app.use(lingua(app, { defaultLocale: 'en', path: __dirname + '/i18n' })); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.static(__dirname + '/public')); app.use(app.router); ... });
Note: Please ensure that the call: "app.use(app.router);" is the last entry in your configuration section.
Create i18n resource files - Note that you have to create a resource file for your default language. (In this example: './i18n/en.json' and './i18n/de-de.json').
// en.json { "title": "Hello World", "content": { "description": "A little description." } } // de-de.json { "title": "Hallo Welt", "content": { "description": "Eine kleine Beschreibung." } }
Use lingua in your views - Note that the syntax depends on your template engine. In this example it is: jqtpl and the request comes from a browser which sends 'en' with the HTTP request header.
<h1>${lingua.title}</h1> <!-- out: <h1>Hello World</h1> --> <p>${lingua.content.description}</h1> <!-- out: <p>A little description.</p> -->
Let the user select a language - Note that the user's selection is persisted within a cookie. This is an optional step. If you want to let lingua determine the user language from the browser configuration then leave this step out. Anyway, this is a very handy feature for switching the language by a user decision.
<a href="?language=de-DE">de-DE</a> <a href="?language=en-US">en-US</a>
Example Application
There is an example application at ./example
To run it:
$ cd example
$ node app.js
You can find a deployed version of this app here.
License
Author
Copyright (c) 2011, André König (Google+) (andre.koenig -[at]- gmail [dot] com)