support i18n
support i18n
This commit is contained in:
parent
86b4da54d4
commit
d33b51432d
10 changed files with 151 additions and 15 deletions
47
src/locale/format.js
Normal file
47
src/locale/format.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* String format template
|
||||
* - Inspired:
|
||||
* https://github.com/Matt-Esch/string-template/index.js
|
||||
*/
|
||||
|
||||
const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
|
||||
|
||||
export default function(Vue) {
|
||||
const { hasOwn } = Vue.util;
|
||||
|
||||
/**
|
||||
* template
|
||||
*
|
||||
* @param {String} string
|
||||
* @param {Array} ...args
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
function template(string, ...args) {
|
||||
if (args.length === 1 && typeof args[0] === 'object') {
|
||||
args = args[0];
|
||||
}
|
||||
|
||||
if (!args || !args.hasOwnProperty) {
|
||||
args = {};
|
||||
}
|
||||
|
||||
return string.replace(RE_NARGS, (match, prefix, i, index) => {
|
||||
let result;
|
||||
|
||||
if (string[index - 1] === '{' &&
|
||||
string[index + match.length] === '}') {
|
||||
return i;
|
||||
} else {
|
||||
result = hasOwn(args, i) ? args[i] : null;
|
||||
if (result === null || result === undefined) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
50
src/locale/index.js
Normal file
50
src/locale/index.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
// https://github.com/ElemeFE/element/blob/dev/src/locale/index.js
|
||||
|
||||
import defaultLang from './lang/zh-CN';
|
||||
import Vue from 'vue';
|
||||
import deepmerge from 'deepmerge';
|
||||
import Format from './format';
|
||||
|
||||
const format = Format(Vue);
|
||||
let lang = defaultLang;
|
||||
let merged = false;
|
||||
let i18nHandler = function() {
|
||||
const vuei18n = Object.getPrototypeOf(this || Vue).$t;
|
||||
if (typeof vuei18n === 'function') {
|
||||
if (!merged) {
|
||||
merged = true;
|
||||
Vue.locale(
|
||||
Vue.config.lang,
|
||||
deepmerge(lang, Vue.locale(Vue.config.lang) || {}, { clone: true })
|
||||
);
|
||||
}
|
||||
return vuei18n.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
export const t = function(path, options) {
|
||||
let value = i18nHandler.apply(this, arguments);
|
||||
if (value !== null && value !== undefined) return value;
|
||||
|
||||
const array = path.split('.');
|
||||
let current = lang;
|
||||
|
||||
for (let i = 0, j = array.length; i < j; i++) {
|
||||
const property = array[i];
|
||||
value = current[property];
|
||||
if (i === j - 1) return format(value, options);
|
||||
if (!value) return '';
|
||||
current = value;
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
export const use = function(l) {
|
||||
lang = l || lang;
|
||||
};
|
||||
|
||||
export const i18n = function(fn) {
|
||||
i18nHandler = fn || i18nHandler;
|
||||
};
|
||||
|
||||
export default { use, t, i18n };
|
8
src/locale/lang/en-US.js
Normal file
8
src/locale/lang/en-US.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
export default {
|
||||
i: {
|
||||
select: {
|
||||
placeholder: 'Select',
|
||||
noMatch: 'No matching data'
|
||||
}
|
||||
}
|
||||
};
|
8
src/locale/lang/zh-CN.js
Normal file
8
src/locale/lang/zh-CN.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
export default {
|
||||
i: {
|
||||
select: {
|
||||
placeholder: '请选择',
|
||||
noMatch: '无匹配数据'
|
||||
}
|
||||
}
|
||||
};
|
8
src/locale/lang/zh-TW.js
Normal file
8
src/locale/lang/zh-TW.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
export default {
|
||||
i: {
|
||||
select: {
|
||||
placeholder: '請選擇',
|
||||
noMatch: '無匹配數據'
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue