Giter VIP home page Giter VIP logo

express-lingua's Introduction

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:

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

  2. 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."
            }
        }
  3. 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> -->
  4. 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

MIT License

Author

Copyright (c) 2011, André König (Google+) (andre.koenig -[at]- gmail [dot] com)

express-lingua's People

Contributors

goloroden avatar

Stargazers

Amalia Hernandez avatar

Watchers

Amalia Hernandez avatar James Cloos 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.