nocobase/packages/client/src/i18n.ts
chenos 479f64f197
feat: support i18n (#99)
* feat: i18next

* multi language data

* feat(client): locale support

* en-US for collections

* us-EN for routes

* en-US for routes

* en-US for attachments

* feat: partial translations

* translation

* add AntdConfigProvider

* translation

* translation

* feat: translation

* feat: add translation

* fix: improve translation

* feat: improve translation

* fix: SyntaxError: Unexpected token ) in JSON at position

* typo

* feat: improve translation

* feat: improve translation

* feat: language settings can be saved on the server

* feat: lang option of init cli

* demo translation

* typo

* change address field to textarea

* address data

* feat: chart translation

* feat: chart translation

* improve translation

* signin,signup,signout...

Co-authored-by: Zhou <zhou.working@gmail.com>
2021-11-08 19:32:59 +08:00

53 lines
1.1 KiB
TypeScript

import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import zhCN from './locale/zh_CN';
import enUS from './locale/en_US';
import moment from 'moment';
export const i18n = i18next.createInstance();
i18n.use(initReactI18next).init({
lng: localStorage.getItem('locale') || 'en-US',
debug: true,
defaultNS: 'client',
parseMissingKeyHandler: (key) => {
console.log('parseMissingKeyHandler', `'${key}': '${key}',`);
return key;
},
// ns: ['client'],
resources: {
'en-US': {
client: {
...enUS,
},
},
'zh-CN': {
client: {
...zhCN,
},
},
},
});
const momentLngs = {
'en-US': 'en',
'zh-CN': 'zh-cn',
};
function setMomentLng(language) {
const lng = momentLngs[language||'en-US'] || 'en';
console.log("localStorage.getItem('locale')", lng);
moment.locale(lng);
}
setMomentLng(localStorage.getItem('locale'));
i18n.on('languageChanged', (lng) => {
localStorage.setItem('locale', lng);
setMomentLng(lng);
});
// export const t = i18n.t;
export default i18n;