{"id":15209087,"url":"https://github.com/koalazak/i18n-express","last_synced_at":"2025-07-17T12:38:27.410Z","repository":{"id":57270326,"uuid":"44889847","full_name":"koalazak/i18n-express","owner":"koalazak","description":"A simple i18n middleware for Express.js","archived":false,"fork":false,"pushed_at":"2020-04-10T13:20:03.000Z","size":25,"stargazers_count":55,"open_issues_count":7,"forks_count":14,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-02T01:32:06.974Z","etag":null,"topics":["cookie","ejs","express","file","geo","geolang","i18n","internationalization","json","json-files","lang","language","language-settings","simple"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koalazak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-25T00:03:25.000Z","updated_at":"2025-01-23T13:28:52.000Z","dependencies_parsed_at":"2022-09-04T19:22:38.609Z","dependency_job_id":null,"html_url":"https://github.com/koalazak/i18n-express","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koalazak%2Fi18n-express","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koalazak%2Fi18n-express/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koalazak%2Fi18n-express/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koalazak%2Fi18n-express/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koalazak","download_url":"https://codeload.github.com/koalazak/i18n-express/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238825785,"owners_count":19537127,"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":["cookie","ejs","express","file","geo","geolang","i18n","internationalization","json","json-files","lang","language","language-settings","simple"],"created_at":"2024-09-28T07:21:12.427Z","updated_at":"2025-02-14T10:31:53.737Z","avatar_url":"https://github.com/koalazak.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# i18n-express\n[![Build Status](https://img.shields.io/travis/koalazak/i18n-express.svg)](https://travis-ci.org/koalazak/i18n-express)\n[![npm version](https://badge.fury.io/js/i18n-express.svg)](http://badge.fury.io/js/i18n-express)\n\nA simple i18n middleware for Express.js\nThis module just reads all the \u003clang\u003e.json files in a directory. Then calculates the user lang and exposes \"texts\" variables in your views with the texts in that json. \n\nBy default, the user will see the site in the language set by the `cookieLangName` session. If the session is not set, the language set by the browser will be used.\n\nIf the user wants to set the language to spanish for example, he would have to visit *http://site.com/?clang=es* (clang is defined at `paramLangName`).\n\nThis can be done by using a html 'select' or any other means you want. Once that is done, the `cookieLangName` session will be updated with the new language and the user will forever see the site in the new language until he decides to set a new language again. \n\nNOTE: When using this module, we recommend also using the [geolang-express](https://github.com/koalazak/geolang-express) module, which sets the `cookieLangName` session to a language based on the visit IP address. \n\n\n## Requirements\n\n  - Node \u003e= 0.12\n  - Express.js\n\n## Instalation\n\n```bash\n$ npm install i18n-express\n```\n\n## Usage\n\n```js\nvar i18n=require(\"i18n-express\");\n\napp.use( i18n(options) );\n```\n\n## Options\n\n- `translationsPath` : *(default: `i18n`)* The path where you store translations json files.\n- `cookieLangName` : *(default: `ulang`)* If you provide a cookie name, try to get user lang from this cookie.\n- `browserEnable` : *(default: `true`)* If enabled, try to get user lang from browser headers.\n- `defaultLang` :  *(default: `en`)* If all others methods fail, use this lang.\n- `paramLangName` :  *(default: `clang`)* Get param to change user lang. ej: visiting 'example.com?clang=es' the lang switchs to 'es'\n- `siteLangs` :  *(default: `['en']`)* Array of supported langs. (posbile values for clang and json files)\n- `textsVarName` : *(default: `texts`)* Name of variable which holds the loaded translations.\n\n## Example\n\n\n Create a directory \"i18n\" with .json files for each lang. Ej:\n - en.json\n - es.json\n - en\\-us.json\n \n With translations like this (en.json):\n\n ```json\n {\n \"WELCOME_MSG\": \"Hi! Welcome!\",\n \"CONTACT_TEXT\": \"More bla\"\n }\n ```\n \n\n In your Express app.js:\n\n```javascript\nvar express = require('express');\nvar path = require('path');\nvar cookieParser = require('cookie-parser');\nvar bodyParser = require('body-parser');\nvar i18n=require(\"i18n-express\"); // \u003c-- require the module\n\nvar indexRoutes = require('./routes/index');\n\nvar app = express();\n\n// view engine setup\napp.set('views', path.join(__dirname, 'views'));\napp.set('view engine', 'ejs');\napp.use(cookieParser());\napp.use(express.static(path.join(__dirname, 'public')));\napp.use(session({\n  secret: 'secret',\n  saveUninitialized: true,\n  resave: true\n}));\n\napp.use(i18n({\n  translationsPath: path.join(__dirname, 'i18n'), // \u003c--- use here. Specify translations files path.\n  siteLangs: [\"en\",\"es\"],\n  textsVarName: 'translation'\n}));\n...\n\napp.use('/', indexRoutes);\n\nmodule.exports = app;\n\n...\n\n```\n\nNow in your ejs view you have `texts` object and `lang` variable with the active language:\n\n```html\n\u003cdiv\u003e\n  Choose your language:\n  \u003cul\u003e\n    \u003cli\u003e\u003ca class=\"\u003c%=(lang==\"es\"?\"active\":\"\")%\u003e\" href=\"/?clang=es\"\u003eSpanish\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca class=\"\u003c%=(lang==\"en\"?\"active\":\"\")%\u003e\" href=\"/?clang=en\"\u003eEnglish\u003c/a\u003e\u003c/li\u003e\n  \u003c/ul\u003e \n\n\t\u003cp\u003e\u003c%=translation.WELCOME_MSG%\u003e\u003c/p\u003e\n  \n\u003c/div\u003e\n```\n\nOr in your handlebars view:\n\n```html\n\u003cdiv\u003e\n  Choose your language:\n  \u003cul\u003e\n    \u003cli\u003e\u003ca href=\"/?clang=es\"\u003eSpanish\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/?clang=en\"\u003eEnglish\u003c/a\u003e\u003c/li\u003e\n  \u003c/ul\u003e \n\n\t\u003cp\u003e{{translation.WELCOME_MSG}}\u003c/p\u003e\n\n\u003c/div\u003e\n```\n\n## License\n\nMIT\n\n## Author\n\n  - [Facu ZAK](https://github.com/koalazak) \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoalazak%2Fi18n-express","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoalazak%2Fi18n-express","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoalazak%2Fi18n-express/lists"}