Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

fastify-i18next

Package Overview
Dependencies
3
Maintainers
1
Versions
2
Issues
File Explorer

Advanced tools

fastify-i18next

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

    0.5.1latest

Version published
Maintainers
1
Yearly downloads
2,436
decreased by-58.28%

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

What is fastify-i18next?

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

Is fastify-i18next popular?

The npm package fastify-i18next receives a total of 28 weekly downloads. As such, fastify-i18next popularity was classified as not popular.

Is fastify-i18next well maintained?

We found that fastify-i18next demonstrated a not healthy version release cadence and project activity. It has 1 open source maintainer collaborating on the project.

Last updated on 05 Jul 2019
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc