Rosetty
Complete Intl/I18n solution for browser and node
Usage
const { rosetty, locales } = require('rosetty');
const { enGB: enLocale } = locales;
const r = rosetty(
{
en: {
dict: {
test: 'This is a test',
},
locale: enLocale,
},
},
'en'
);
console.log(r.t('test'));
API
rosetty(config, defaultLang?)
Options
Field Name | Type | Description |
---|
config | Record<string, Language> | Specify dictionnary and locale to use for each lang |
defaultLang | string? | Specify default language to use (should be the same as config) |
defaultLang | boolean? | Return fallback if translation is not defined |
Return
locales
Return: Record<string, Locale>
Return Date-fns locale files. https://date-fns.org/v2.28.0/docs/Locale
WARNING FOR NODE JS ENVIRONMENT
You need to load polyfill on node environment because Intl API is not present. Please use below code to make it works.
export const loadPolyfill = () => {
if (!Intl?.DisplayNames) {
require(`@formatjs/intl-displaynames/polyfill`);
}
if (!Intl?.ListFormat) {
require(`@formatjs/intl-listformat/polyfill-force`);
}
if (!Intl?.NumberFormat) {
require(`@formatjs/intl-numberformat/polyfill`);
}
if (!Intl?.PluralRules) {
require(`@formatjs/intl-pluralrules/polyfill`);
}
};
export const loadPolyfillData = (lang: string) => {
try {
require(`@formatjs/intl-displaynames/locale-data/${lang}`);
} catch (error) {}
try {
require(`@formatjs/intl-listformat/locale-data/${lang}`);
} catch (error) {}
try {
require(`@formatjs/intl-numberformat/locale-data/${lang}`);
} catch (error) {}
try {
require(`@formatjs/intl-pluralrules/locale-data/${lang}`);
} catch (error) {}
try {
require(`@formatjs/intl-displaynames/locale-data/${lang.split('-')[0]}`);
} catch (error) {}
try {
require(`@formatjs/intl-listformat/locale-data/${lang.split('-')[0]}`);
} catch (error) {}
try {
require(`@formatjs/intl-numberformat/locale-data/${lang.split('-')[0]}`);
} catch (error) {}
try {
require(`@formatjs/intl-pluralrules/locale-data/${lang.split('-')[0]}`);
} catch (error) {}
};
Maintain
This package use TSdx. Please check documentation to update this package.