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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue