[improvement] use i18n (#2454)

This commit is contained in:
neverland 2019-01-07 15:24:34 +08:00 committed by GitHub
parent ed7febc131
commit c6e1aaf5da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 6 deletions

View File

@ -17,7 +17,8 @@ module.exports = function (api) {
'@vue/babel-preset-jsx', '@vue/babel-preset-jsx',
{ {
vModel: true, vModel: true,
injectH: true injectH: true,
functional: false
} }
] ]
], ],

View File

@ -1,19 +1,23 @@
/* eslint-disable import/no-mutable-exports */
import Vue from 'vue'; import Vue from 'vue';
import deepAssign from '../utils/deep-assign'; import deepAssign from '../utils/deep-assign';
import defaultMessages from './lang/zh-CN'; import defaultMessages from './lang/zh-CN';
let lang = 'zh-CN';
const proto = Vue.prototype; const proto = Vue.prototype;
const defaultLang = 'zh-CN'; const messages = { [lang]: defaultMessages };
const locale = { const locale = {
install() { install() {
if (proto.$vantLang) { if (proto.$vantLang) {
return; return;
} }
Vue.util.defineReactive(proto, '$vantLang', defaultLang); Vue.util.defineReactive(proto, '$vantLang', lang);
Vue.util.defineReactive(proto, '$vantMessages', { [defaultLang]: defaultMessages }); Vue.util.defineReactive(proto, '$vantMessages', messages);
}, },
use(lang, messages) { use(newLang, messages) {
lang = newLang;
proto.$vantLang = lang; proto.$vantLang = lang;
this.add({ [lang]: messages }); this.add({ [lang]: messages });
}, },
@ -26,3 +30,4 @@ const locale = {
locale.install(); locale.install();
export default locale; export default locale;
export { lang, messages };

View File

@ -0,0 +1,10 @@
import { get, camelize } from '..';
import { lang, messages } from '../../locale';
export default name => {
const prefix = camelize(name) + '.';
return (path, ...args) => {
const message = get(messages[lang], prefix + path) || get(messages[lang], path);
return typeof message === 'function' ? message(...args) : message;
};
};

View File

@ -1,7 +1,8 @@
import useBem from './bem'; import useBem from './bem';
import useSfc from './sfc'; import useSfc from './sfc';
import useI18n from './i18n';
export default function (name) { export default function (name) {
name = 'van-' + name; name = 'van-' + name;
return [useSfc(name), useBem(name)]; return [useSfc(name), useBem(name), useI18n(name)];
} }