Git Product home page Git Product logo

fastify-i18n's Introduction

fastify-i18n

Internationalization plugin for Fastify. Built upon node-polyglot.

Installation

Install fastify-i18n with your favorite package manager:

$ npm i fastify-i18n
# or
$ yarn add fastify-i18n
# or
$ pnpm i fastify-i18n
# or
$ bun add fastify-i18n

Usage

// esm
import i18n, { defineI18n, useI18n } from 'fastify-i18n';

// cjs
const { default: i18n, defineI18n, useI18n } = require('fastify-i18n');

Global Scope

import i18n from 'fastify-i18n';

fastify.register(i18n, {
  fallbackLocale: 'en',
  messages: {
    en: { text: 'Text' },
    ja: { text: 'テキスト' },
    ko: { text: '텍스트' },
    zh: { text: '文字' },
  },
});

/*
  curl --request GET \
    --url http://127.0.0.1:3000/api/i18n \
    --header 'accept-language: ja'
  */
fastify.get('/api/i18n', async (req, reply) => {
  return reply.send({ message: req.i18n.t('text') });
});

Local Scope

import type { FastifyInstance } from 'fastify';
import { defineI18n, useI18n } from 'fastify-i18n';

export default async (app: FastifyInstance) => {
  defineI18n(app, {
    en: { hello: 'Hello, World!' },
    ja: { hello: 'こんにちは世界!' },
    ko: { hello: '안녕하세요, 월드입니다!' },
    zh: { hello: '你好,世界!' },
  });

  /*
  curl --request GET \
    --url http://127.0.0.1:3000/api/hello-world \
    --header 'accept-language: ja'
  */
  app.get('/hello-world', async (req, reply) => {
    const i18n = useI18n(req);

    return reply.send({
      // local scope
      hello: i18n.t('hello'),

      // global scope
      text: req.i18n.t('text'),
    });
  });
};

Automatic Conversion

xx-XX to xx

fastify.register(i18n, {
  fallbackLocale: 'en',
  messages: {
    en: { text: 'Text' },
    ja: { text: 'テキスト' },
  },
});
$ curl --request GET \
       --url http://127.0.0.1:3000/api/i18n \
       --header 'Accept-Language: ja-JP'
# Output: { text: 'テキスト' } (ja)

xx to xx-XX

fastify.register(i18n, {
  fallbackLocale: 'en',
  messages: {
    'en-US': { text: 'Text' },
    'ja-JP': { text: 'テキスト' },
  },
});
$ curl --request GET \
       --url http://127.0.0.1:3000/api/i18n \
       --header 'Accept-Language: ja'
# Output: { text: 'テキスト' } (ja-JP)

Priority

fastify.register(i18n, {
  fallbackLocale: 'en',
  messages: {
    'en-US': { text: 'Text' },
    'zh-CN': { text: '文本' },
    'zh-TW': { text: '文字' },
  },
});
$ curl --request GET \
       --url http://127.0.0.1:3000/api/i18n \
       --header 'Accept-Language: zh'
# Output: { text: '文本' } (zh-CN)
fastify.register(i18n, {
  fallbackLocale: 'en',
  messages: {
    'en-US': { text: 'Text' },
    'zh-TW': { text: '文字' },
    'zh-CN': { text: '文本' },
  },
});
$ curl --request GET \
       --url http://127.0.0.1:3000/api/i18n \
       --header 'Accept-Language: zh'
# Output: { text: '文字' } (zh-TW)

fastify-i18n's People

Contributors

shyam-chen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.