Skip to content

beliven-it/fastify-polyglot

Repository files navigation

fastify-polyglot

A plugin to handle i18n using node-polyglot

Node.js CI

Install

$ npm i --save fastify-polyglot

Usage

const path = require('path')

fastify.register(require('fastify-polyglot'), {
  defaultLocale: 'it',
  locales: {
    it: require(path.join(__dirname, './locales/it'))
  }
})

console.log(fastify.i18n.t('Hello!'))

// 'Ciao!'

Alternatively, you can pass a path lo load locales from:

const path = require('path')

fastify.register(require('fastify-polyglot'), {
  defaultLocale: 'it',
  localesPath: path.join(__dirname, './locales')
})

Options

Name Description
defaultLocale The default locale code to be used (en by default).
localesPath The folder from where to load locale dictionaries (./locales by default).
locales A map of locales, where keys are locale codes and values are translation dictionaries.

NOTE: if both localesPath and locales are passed, dictionaries will be merged together.

Test

$ npm test

Acknowledgements

This project is kindly sponsored by:

Beliven

License

Licensed under MIT