i18n-express-urlbased
This project is based on https://github.com/koalazak/i18n-express
A multi-language Middleware for ExpressJS, which loads the translations based on the URL requested.
This module requires that Express uses some Session module.
const i18n = require('i18n-express-urlbased')
const express-session = require('express-session')
const app = express()
app.use(session({
resave: true,
saveUninitialized: false,
secret: "secret"
}))
app.use(i18n())
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
How to create a translation file
A translation file would be stored on the translations folder (default: i18n
). Each language has it's own JSON file (ie. en.json
).
The JSON structure is based on the URL Mapping as the following example shows. Each time a controlled is called, the translation is going to be loaded into the rendered variables.
{
"/": {
"title": "Index title"
},
"/otherPage": {
"title": "Other page title",
"message": "..."
},
"/mystory": {
"title": "My life story title",
"decades": {
"birth": "During my childbirth ...",
"1990": "During this ...",
"2000": "During this decade ...",
}
}
}
How to load it into my page
PugJS
h1= translation.title
.div
p= translation.decades.birth
EJS
<h1>
<%=translation.title%>
</h1>
<div>
<p>
<%=translation.decades.birth%>
</p>
</div>
Available Options
{
path: "i18n", // Path to translations' folder
defaultLang: "en", // Default language to be used if the user has no configured cookie or session
paramLangName: "clang", // Parameter on which the Express will listen to change language settings
cookieLangName: "ulang" // Cookie in which the language preference is stored
}