{"id":13519741,"url":"https://github.com/akoenig/express-lingua","last_synced_at":"2025-04-10T11:42:29.403Z","repository":{"id":1726215,"uuid":"2457074","full_name":"akoenig/express-lingua","owner":"akoenig","description":"An i18n middleware for the Express.js framework.","archived":false,"fork":false,"pushed_at":"2014-03-10T13:39:01.000Z","size":270,"stargazers_count":67,"open_issues_count":6,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-15T12:17:04.862Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akoenig.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-09-25T23:16:26.000Z","updated_at":"2021-11-18T06:29:49.000Z","dependencies_parsed_at":"2022-09-02T14:31:55.991Z","dependency_job_id":null,"html_url":"https://github.com/akoenig/express-lingua","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akoenig%2Fexpress-lingua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akoenig%2Fexpress-lingua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akoenig%2Fexpress-lingua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akoenig%2Fexpress-lingua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akoenig","download_url":"https://codeload.github.com/akoenig/express-lingua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248211608,"owners_count":21065791,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:02:02.641Z","updated_at":"2025-04-10T11:42:29.383Z","avatar_url":"https://github.com/akoenig.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Software"],"sub_categories":["Utilities"],"readme":"# Lingua\n\nLingua 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.\n\n## Installation\n\n    $ npm install lingua\n\n## Quick Start\n\nUsing lingua comes down with four simple steps:\n\n1. **Grab lingua**\n\n    ```javascript\n\n    var express = require('express'),\n        lingua  = require('lingua');\n\n    ...\n    // Express init code goes here\n    ...    \n\n    // Express app configuration code and lingua init.\n    app.configure(function() {\n        ...\n        app.set('views', __dirname + '/views');\n        app.set('view engine', 'ejs');\n\n        // Lingua configuration\n        app.use(lingua(app, {\n            defaultLocale: 'en',\n            path: __dirname + '/i18n'\n        }));\n\n        app.use(express.bodyParser());\n        app.use(express.methodOverride());\n        app.use(express.static(__dirname + '/public'));\n        app.use(app.router);\n\n        ...\n    });\n    ```\n    _Note:_ Please ensure that the call: \"app.use(app.router);\" is the last entry in your configuration section.\n\n2. **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').\n\n    ```javascript\n    // en.json\n        {\n            \"title\": \"Hello World\",\n            \"content\": {\n                \"description\": \"A little description.\"\n            }\n        }\n\n    // de-de.json\n        {\n            \"title\": \"Hallo Welt\",\n            \"content\": {\n                \"description\": \"Eine kleine Beschreibung.\"\n            }\n        }\n    ```\n\n3.    \n    a.  **Use lingua in your views - Static output** - Note that the syntax depends on your template engine. In this example it is: [ejs](http://embeddedjs.com/) and the request comes from a browser which sends 'en' with the HTTP request header.\n\n    ```html\n    \u003ch1\u003e\u003c%= lingua.title %\u003e\u003c/h1\u003e \u003c!-- out: \u003ch1\u003eHello World\u003c/h1\u003e --\u003e\n    \u003cp\u003e\u003c%= lingua.content.description %\u003e\u003c/h1\u003e \u003c!-- out: \u003cp\u003eA little description.\u003c/p\u003e --\u003e\n    ```\n\n    b.  **Use lingua in your views - Dynamic output** - Sometimes it is necessary to handle dynamic data within your express route and pass it to the template. What if your i18n resource includes placeholders (\"{key}\") within a string where you can put in your dynamic data? Well, it is possible. First of all, look at this i18n resource file:\n\n    ```javascript\n    // de.json\n    {\n        \"greeting\": \"Hallo {name}. Dieser Schlüssel {code} wurde für Dich generiert.\"\n    }\n    ```\n\n    Now it is possible to transfer an object from your route into your template:\n\n    ```javascript\n    app.get('/', function(req, res) {\n        var names = ['Sarah', 'Thomas', 'Claudia'];\n\n        res.render('index', {\n            person: {\n                name: names[Math.floor(Math.random()*names.length)],\n                code: Math.round(Math.random()*100)\n            }\n        });\n    });\n    ```\n\n    And finally you can execute the i18n resource (yes, you can execute it (: ) and pass your data model to this function:\n\n    ```html\n    \u003cp\u003e\u003c%= lingua.greeting(person) %\u003e\u003c/p\u003e\n    ```\n\n    _Note:_ Every i18n resource which contains placeholders like in the example above is a function after you've started the application.\n\n4.  **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.\n\n    ```html\n    \u003ca href=\"?language=de-DE\"\u003ede-DE\u003c/a\u003e\n    \u003ca href=\"?language=en-US\"\u003een-US\u003c/a\u003e\n    ```\n\n    You can configure lingua in order to change the name of this parameter.\n\n    ```javascript\n\n    // Express app configuration code and lingua init.\n    app.configure(function() {\n        ...\n\n        // Lingua configuration\n        app.use(lingua(app, {\n            defaultLocale: 'en',\n            path: __dirname + '/i18n',\n            storageKey: 'lang' // http://domain.tld/?lang=de\n        }));\n\n        ...\n    });\n    ```\n\n    The cookie lingua uses expires in one year, and includes the httpOnly flag to prevent clientside access from Javascript. You can override these settings by providing a cookieOptions key during configuration.\n\n    ```javascript\n\n    // Express app configuration code and lingua init.\n    app.configure(function() {\n        ...\n\n        // Lingua configuration\n        app.use(lingua(app, {\n            defaultLocale: 'en',\n            path: __dirname + '/i18n',\n            storageKey: 'lang', // http://domain.tld/?lang=de\n            cookieOptions: {\n                domain: '.domain.tld',    // to allow subdomains access to the same cookie, for instance\n                path: '/blog',            // to restrict the language cookie to a path\n                httpOnly: false,          // if you need access to this cookie from javascript on the client\n                expires: new Date(Date.now() + 24 * 60 * 60 * 1000),  // expire in 1 day instead of 1 year\n                secure: true              // for serving over https\n            }\n        }));\n\n        ...\n    });\n    ```\n\n    \n\n\n## Example Application\n\nThere is an example application at [./example](https://github.com/akoenig/express-lingua/tree/master/example)\n\nTo run it:\n\n    $ cd example\n    $ npm i\n    $ node app.js\n\n\n## License\n\n[MIT License](http://www.opensource.org/licenses/mit-license.php)\n\n## Author\n\nCopyright (c) 2013, [André König](http://iam.andrekoenig.info)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakoenig%2Fexpress-lingua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakoenig%2Fexpress-lingua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakoenig%2Fexpress-lingua/lists"}