This is a plugin for translation and internationalization. It is build on top of ilingo.
Table of Contents
npm install @routup/i18n --save
To read the docs, visit https://routup.net
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);
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;
Made with 💚
Published under MIT License.