mirror of https://github.com/ElemeFE/element
Locale: add format
parent
56b23359d3
commit
472a7ef916
|
@ -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;
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
import defaultLang from 'element-ui/src/locale/lang/zh-cn';
|
||||
import Vue from 'vue';
|
||||
import Format from './format';
|
||||
|
||||
const format = Format(Vue);
|
||||
let lang = defaultLang;
|
||||
|
||||
export const $t = function(path, options) {
|
||||
|
@ -13,7 +16,7 @@ export const $t = function(path, options) {
|
|||
for (var i = 0, j = array.length; i < j; i++) {
|
||||
var property = array[i];
|
||||
var value = current[property];
|
||||
if (i === j - 1) return value;
|
||||
if (i === j - 1) return format(value, options);
|
||||
if (!value) return '';
|
||||
current = value;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue