Socket
Socket
Sign inDemoInstall

koa-i18next-middleware

Package Overview
Dependencies
7
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    koa-i18next-middleware

An i18next middleware for koa2 framework.


Version published
Weekly downloads
1.2K
increased by1.97%
Maintainers
1
Install size
827 kB
Created
Weekly downloads
 

Readme

Source

koa-i18next-middleware

NPM Version Coverage Status Build Status Downloads Dependency Status License

An i18next middleware for koa2 framework.

Install

npm i -S koa-i18next-middleware

Usage

Notice: this plugin requires koa2 with async enabled in Node.js.
const i18next = require('i18next');
const i18m = require('koa-i18next-middleware');

// add custom detector.
const lngDetector = new i18m.LanguageDetector();
lngDetector.addDetector({
    name: 'mySessionDetector',

    lookup(ctx, options) {
        let found;
        if (options.lookupSession && ctx && ctx.sessions) {
            found = ctx.sessions[options.lookupMySession];
        }
        return found;
    },

    cacheUserLanguage(ctx, lng, options = {}) {
        if (options.lookupMySession && ctx && ctx.session) {
            ctx.session[options.lookupMySession] = lng;
        }
    }
});

i18next.use(lngDetector).init(
    {
        fallbackLng: 'en',
        preload: ['en', 'es'],
        resources: {
            en: {
                translation: {
                    key: 'hello world'
                }
            },
            es: {
                translation: {
                    key: 'es hello world es'
                }
            }
        },
        detection: {
            order: [
                'querystring',
                'path',
                /*'cookie', 'header',*/ 'session',
                'mySessionDetector'
            ],

            lookupQuerystring: 'lng',

            lookupParam: 'lng', // for route like: 'path1/:lng/result'
            lookupFromPathIndex: 0,

            // currently using ctx.cookies
            lookupCookie: 'i18next',
            // cookieExpirationDate: new Date(), // default: +1 year
            // cookieDomain: '', // default: current domain.

            // currently using ctx.session
            lookupSession: 'lng',

            // other options
            lookupMySession: 'lang',

            // cache user language
            caches: ['cookie', 'mySessionDetector']
        }
    },
    (err, t) => {
        // initialized and ready to go!
        const hw = i18next.t('key'); // hw = 'hello world'
        console.log(hw);
    }
);

app.use(
    i18m.getHandler(i18next, {
        locals: 'locals',
        ignoreRoutes: ['/no-lng-route']
    })
);

License

MIT © steven MIT © i18next-express-middleware

Keywords

FAQs

Last updated on 03 Jun 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc