Skip to main content

Recipes

Translate with interpolation

import { getI18n } from '@quik/core';

const message = getI18n().translate('app.welcome', { name: 'Ada' });

Interpolation replaces {{name}} placeholders in the translated string with the given parameters.

Get a translator bound to one language

translator(language?) returns an IQTranslator whose translate(key, parameters) always resolves against the given language, regardless of the caller's request language:

import { getI18n } from '@quik/core';

const fr = getI18n().translator('fr');
const message = fr.translate('app.welcome', { name: 'Ada' });

Load translations from an object instead of a file

import { QI18n } from '@quik/i18n';

const i18n = new QI18n({ supportedLanguages: [ 'en' ] });
i18n.load('en', { app: { welcome: 'Welcome, {{name}}!' } });
await i18n.setup();

Read and edit individual keys at runtime

import { getI18n } from '@quik/core';

getI18n().set('en', 'app.welcome', 'Hi, {{name}}!');
getI18n().get('en', 'app.welcome');
getI18n().remove('en', 'app.welcome');

Wire language detection into an Express app

import { getI18n } from '@quik/core';

getI18n().setupHttpEngine(app, 'express');

This registers the i18next-http-middleware handler, which detects the request language from the lang querystring, the i18n.cookie cookie, or the Accept-Language header, in that order.