Simple package to manage translations in Node JS apps wyth AsyncLocalStorage api.
This is a Node.js module available through the npm registry. Installation is done using the npm install command:
npm install http-locales
Load the translations files inside the default folder locales
. It can be changed using the env variable: process.env.HTTP_LOCALES_FOLDER.
import { HttpLocales } from "http-locales"
await HttpLocales.load()
Create the AsyncLocaleStorage containing a lang.
// Here we are using en language. It will take the translations from locales/en.json.
HttpLocalesContext.run("en", function() {
// ...
})
__("hello %s", "arnaud")
// Will print hello arnaud
import express, { NextFunction } from "express"
import { HttpLocales, HttpLocalesContext, __ } from "http-locales"
await HttpLocales.load()
const app = express()
app.use((_, __, next: NextFunction) => {
HttpLocalesContext.run("en", function() {
next()
})
})
app.get("/", (_, res) => {
res.send(__("hello"))
})
app.listen(3000)