intl-core
@cloudflare/intl-core
This package provides core utilities for i18n.
Installation
$ npm install @cloudflare/intl-core
Translator
This is a wrapper around the functions of Polyglot.
The main benefit it provides is that it stores various instances of Polyglot for
each locale in order to be able to locate translations which are not found in
target locale but are found in English locale.
import Translator from '@cloudfalre/intl-core';
const phrases = {
phrase_key: 'phrase_value',
second_phrase: 'second_value'
};
const germanPhrases = {
phrase_key: 'german_value'
};
const translator = new Translator({
phrases
});
translator.locale(ESupportedLocales.de_DE);
translator.extend(germanPhrases);
translator.t('phrase_key');
translator.t('second_phrase');
makeLoadPhrases
This is a function which takes an object mapping each locale
to a function
returning a Promise which resolves to the catalog of phrases for that locale
.
The main usecase for this is to provide a way to asynchronously load phrases
based on locale
. You can include an index.ts
file inside your locale
directory with the following content:
import { makeLoadPhrases, ESupportedLocales } from '@cloudflare/intl-core';
const loadPhrases = makeLoadPhrases({
[ESupportedLocales.en_US]: () => import('./en-US/phrases.json')
});
export default loadPhrases;