Socket
Socket
Sign inDemoInstall

fastify-i18next

Package Overview
Dependencies
7
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fastify-i18next

i18next plugin for fastify, based on i18next-express-middleware


Version published
Weekly downloads
4
decreased by-50%
Maintainers
1
Install size
438 kB
Created
Weekly downloads
 

Readme

Source

Introduction

This is a plugin to use i18next in Fastify, based on i18next-express-middleware.

Getting started

Source can be loaded via npm.

# npm package
$ npm install fastify-i18next

wire up i18next to request object

var i18next = require("i18next");
var fastifyi18next = require("fastify-i18next");
var fastify = require("fastify")({ logger: true });

i18next.use(fastifyi18next.LanguageDetector).init({
  preload: ["en", "de"],
  resources: {
    en: {
      translation: {
        "title": "Hello World!"
      }
    },
    de: {
      translation: {
        "title": "Hallo Welt!"
      }
    }
  },
  ...otherOptions
});

fastify.register(fastifyi18next.plugin, { i18next });

fastify.get('/', async (request, reply) => {
  return request.t('title');
});

const start = async () => {
  try {
    await fastify.listen(3000)
    fastify.log.info(`server listening on ${fastify.server.address().port}`)
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

language detection

Detects user language from current request. Comes with support for:

  • path
  • cookie
  • header
  • querystring
  • session

Wiring up:

var i18next = require("i18next");
var fastifyi18next = require("fastify-i18next");

i18next.use(fastifyi18next.LanguageDetector).init(i18nextOptions);

As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.

Detector Options

{
  // order and from where user language should be detected
  order: [/*'path', 'session', */ 'querystring', 'cookie', 'header'],

  // keys or params to lookup language from
  lookupQuerystring: 'lng',
  lookupCookie: 'i18next',
  lookupHeader: 'accept-language',
  lookupSession: 'lng',
  lookupPath: 'lng',
  lookupFromPathIndex: 0,

  // cache user language
  caches: false, // ['cookie']

  // optional expire and domain for set cookie
  cookieExpirationDate: new Date(),
  cookieDomain: 'myDomain',
  cookieSecure: true // if need secure cookie
}

Options can be passed in:

preferred - by setting options.detection in i18next.init:

var i18next = require("i18next");
var fastifyi18next = require("fastify-i18next");

i18next.use(fastifyi18next.LanguageDetector).init({
  detection: options
});

on construction:

var fastifyi18next = require("fastify-i18next");
var lngDetector = new fastifyi18next.LanguageDetector(null, options);

via calling init:

var fastifyi18next = require("fastify-i18next");

var lngDetector = new fastifyi18next.LanguageDetector();
lngDetector.init(options);

Adding own detection functionality

interface

module.exports {
  name: 'myDetectorsName',

  lookup: function(req, res, options) {
    // options -> are passed in options
    return 'en';
  },

  cacheUserLanguage: function(req, res, lng, options) {
    // options -> are passed in options
    // lng -> current language, will be called after init and on changeLanguage

    // store it
  }
};

adding it

var i18next = require("i18next");
var fastifyi18next = require("fastify-i18next");

var lngDetector = new fastifyi18next.LanguageDetector();
lngDetector.addDetector(myDetector);

i18next.use(lngDetector).init({
  detection: options
});

Keywords

FAQs

Last updated on 05 Jul 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