mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] use i18n (#2454)
This commit is contained in:
parent
ed7febc131
commit
c6e1aaf5da
@ -17,7 +17,8 @@ module.exports = function (api) {
|
||||
'@vue/babel-preset-jsx',
|
||||
{
|
||||
vModel: true,
|
||||
injectH: true
|
||||
injectH: true,
|
||||
functional: false
|
||||
}
|
||||
]
|
||||
],
|
||||
|
@ -1,19 +1,23 @@
|
||||
/* eslint-disable import/no-mutable-exports */
|
||||
import Vue from 'vue';
|
||||
import deepAssign from '../utils/deep-assign';
|
||||
import defaultMessages from './lang/zh-CN';
|
||||
|
||||
let lang = 'zh-CN';
|
||||
const proto = Vue.prototype;
|
||||
const defaultLang = 'zh-CN';
|
||||
const messages = { [lang]: defaultMessages };
|
||||
|
||||
const locale = {
|
||||
install() {
|
||||
if (proto.$vantLang) {
|
||||
return;
|
||||
}
|
||||
Vue.util.defineReactive(proto, '$vantLang', defaultLang);
|
||||
Vue.util.defineReactive(proto, '$vantMessages', { [defaultLang]: defaultMessages });
|
||||
Vue.util.defineReactive(proto, '$vantLang', lang);
|
||||
Vue.util.defineReactive(proto, '$vantMessages', messages);
|
||||
},
|
||||
|
||||
use(lang, messages) {
|
||||
use(newLang, messages) {
|
||||
lang = newLang;
|
||||
proto.$vantLang = lang;
|
||||
this.add({ [lang]: messages });
|
||||
},
|
||||
@ -26,3 +30,4 @@ const locale = {
|
||||
locale.install();
|
||||
|
||||
export default locale;
|
||||
export { lang, messages };
|
||||
|
10
packages/utils/use/i18n.js
Normal file
10
packages/utils/use/i18n.js
Normal 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;
|
||||
};
|
||||
};
|
@ -1,7 +1,8 @@
|
||||
import useBem from './bem';
|
||||
import useSfc from './sfc';
|
||||
import useI18n from './i18n';
|
||||
|
||||
export default function (name) {
|
||||
name = 'van-' + name;
|
||||
return [useSfc(name), useBem(name)];
|
||||
return [useSfc(name), useBem(name), useI18n(name)];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user