Skip to content

2-towns/http-locales

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http locales

Simple package to manage translations in Node JS apps wyth AsyncLocalStorage api.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

npm install http-locales

API

HttpLocales.load()

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()

HttpLocalesContext.run(lang,callback)

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() {
    // ... 
})

__(text,interpolations?)

__("hello %s", "arnaud")
// Will print hello arnaud

Full example for express

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)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published