i18n.js 428 B

1234567891011121314
  1. import store from './store.js'
  2. const locale = (await import(`./i18n/${store.get('locale') || 'zh-cn'}.js`)).default
  3. export default function(key, ...args){
  4. try{
  5. const msg = key.split('.').reduce((val, k) => val[k], locale);
  6. if(msg && args.length > 0){
  7. return msg.replace(/\{(\d+)\}/g, (match, index) => args[index])
  8. }
  9. return msg || key;
  10. } catch(e){
  11. return key
  12. }
  13. }