@routup/i18n
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

@routup/i18n

npm version main codecov Known Vulnerabilities Conventional Commits

This is a plugin for translation and internationalization. It is build on top of ilingo.

Table of Contents

Installation

npm install @routup/i18n --save

Documentation

To read the docs, visit https://routup.net

Usage

It is important to install the plugin, to enable locale detection and translator usage.

import { createServer } from 'node:http';
import { MemoryStore } from 'ilingo';
import {
    createNodeDispatcher,
    coreHandler,
    Router
} from 'routup';
import {
    i18n,
    useTranslator
} from '@routup/i18n';

const store = new MemoryStore({
    data: {
        de: {
            app: {
                key: 'Hallo, mein Name ist {{name}}',
            },
        },
        en: {
            app: {
                key: 'Hello, my name is {{name}}',
            },
        },
    },
})

const router = new Router();

router.use(i18n({ store }));

router.get('/', coreHandler((req, res) => {
    const translator = useTranslator(req);
    const translation = translator({
        group: 'app',
        key: 'key',
        data: {
            name: 'Peter'
        }
    });
    console.log(translation);
    // Hallo, mein Name ist Peter

    return translation;
}));

const server = createServer(createNodeDispatcher(router));
server.listen(3000);

Types

Helpers

useTranslator

This function returns a translator function to receive a translation for a given key.

import { Request, Translator } from '@routup/i18n';

declare function useTranslator(
    req: Request
): Translator;

License

Made with 💚

Published under MIT License.

Package Sidebar

Install

npm i @routup/i18n

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

26.2 kB

Total Files

17

Last publish

Collaborators

  • tada5hi