Skip to content

clqu/express-translation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@clqu/express-translation

NPM Version NPM Downloads

Installation

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

$ npm install @clqu/express-translation express-session

express-session is required module.

Usage

const app = require("express")();
const { Translation } = require("@clqu/express-translation");

const session = require("express-session"); // required module
const translation = new Translation({
    path: "./locales/%file", // ./locales/en-US.json
    defaultLanguage: "en-US"
});

// Sequence is important, import after session.
app.set('trust proxy', 1);
app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie: { 
        secure: false 
    },
}));

app.use(translation.use);

app.get("/", (req, res) => {
    res.send(req.t("hello"));
});

app.get("/languages", (req, res) => {
    res.send(req.getLanguages());
});

app.get('/addLanguage', (req, res) => {
    req.addLanguage('tr-TR');
    res.send('Added language');
});

app.get('/setLanguage', (req, res) => {
    req.setLanguage('tr-TR');
    res.send(req.t('hello'));
});

app.listen(3000, () => {
    console.log("Server started on port 3000");
});

Interface

interface Request {
    t: (key: any) => void;
    language: string;
    addLanguage: (language: string) => void;
    getLanguages: () => void;
    setLanguage: (language: string) => void;
}

License

MIT