Prepare
Create directory locale. Create 3 files below.
index.ts
import { getI18n } from '../i18n';
export const i18n = getI18n({
messages: locale => require(`./locale.${locale}.json`)
});locale.en.json
{
"greeting": "Hello",
"lol": "hello {someone}. My name is {myName}"
}
locale.vi.json
{
"greeting": "Xin chao",
"lol": "chao {someone}. Ten toi la {myName}"
}
main app
import { setGlobalLocale } from 'i18n-wrapper';
import { i18n } from './locale';
setGlobalLocale({ locale: 'en' });
const { lol } = i18n.messages();
const lolContent = lol({ someone: 'John', myName: 'David' });
console.log(lolContent);