npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-i18n

v3.0.0

Published

Internationalization plugin for Fastify. Built upon node-polyglot.

Downloads

2,363

Readme

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({
      // global scope
      text: req.i18n.t('text'),

      // local scope
      hello: i18n.t('hello'),
    });
  });
};

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)

Glob Import (Vite Only)

// global
fastify.register(i18n, {
  fallbackLocale: 'en-US',
  messages: import.meta.glob(['~/locales/*.ts'], { eager: true }),
});
// local scope
defineI18n(app, import.meta.glob(['./locales/*.ts'], { eager: true }));

Type-safe Resources

Coming soon...

V2 Migration Guide

This guide is intended to help with migration from fastify-i18n v1 to v2.

V2 no longer distinguishes between scopes; each scope's language will override the previous layer's key with the same scope. So, useI18n is no longer needed; you can now access the instance of node-polyglot from fastify.i18n or request.i18n.

app.get('/hello-world', async (req, reply) => {
- const i18n = useI18n(req);

  return reply.send({
    // global scope
    text: req.i18n.t('text'),

    // local scope
-   hello: i18n.t('hello'),
+   hello: req.i18n.t('hello'),
  });
});

Please note that i18n must be registered before your routes, otherwise the global scope will trigger only at the last layer unless you require it.

fastify.register(i18n);
fastify.register(router);

V3 Migration Guide

Support Fastify v5