Comparing version 3.0.0 to 3.1.0
@@ -0,1 +1,23 @@ | ||
<a name="3.1.0"></a> | ||
# [3.1.0](https://github.com/kazupon/vue-i18n/compare/v3.0.0...v3.1.0) (2016-05-09) | ||
### :star: New Features | ||
* component locales ([12fe695](https://github.com/kazupon/vue-i18n/commit/12fe695)), closes [#29](https://github.com/kazupon/vue-i18n/issues/29) | ||
### :warning: Depcreted | ||
* **options:** remove Vue.use options ([d87b59b](https://github.com/kazupon/vue-i18n/commit/d87b59b)) | ||
### :zap: Improvements | ||
* **keypath:** port the object path parser ([3ae04b7](https://github.com/kazupon/vue-i18n/commit/3ae04b7)) | ||
* **translation:** fix hypenate included key translating ([d0a415f](https://github.com/kazupon/vue-i18n/commit/d0a415f)), closes [#24](https://github.com/kazupon/vue-i18n/issues/24) | ||
* **translation:** warning outputing when cannot translate with keypath ([b4c7c0e](https://github.com/kazupon/vue-i18n/commit/b4c7c0e)), closes [#22](https://github.com/kazupon/vue-i18n/issues/22) | ||
<a name="3.0.0"></a> | ||
@@ -2,0 +24,0 @@ # [3.0.0](https://github.com/kazupon/vue-i18n/compare/v2.4.1...v3.0.0) (2016-04-18) |
/*! | ||
* vue-i18n v3.0.0 | ||
* vue-i18n v3.1.0 | ||
* (c) 2016 kazuya kawaguchi | ||
@@ -82,24 +82,2 @@ * Released under the MIT License. | ||
/** | ||
* each | ||
* | ||
* @param {Array|Object} target | ||
* @param {Function} iterator | ||
* @param {Object} [context] | ||
*/ | ||
function each(target, iterator, context) { | ||
if (Array.isArray(target)) { | ||
for (var i = 0; i < target.length; i++) { | ||
iterator.call(context || target[i], target[i], i); | ||
} | ||
} else if (exports$1.Vue.util.isPlainObject(target)) { | ||
for (var key in target) { | ||
if (hasOwn(target, key)) { | ||
iterator.call(context || target[key], target[key], key); | ||
} | ||
} | ||
} | ||
} | ||
var Watcher = void 0; | ||
@@ -148,3 +126,313 @@ /** | ||
// export default for holding the Vue reference | ||
var exports$2 = {}; | ||
// cache | ||
var pathCache = Object.create(null); | ||
// actions | ||
var APPEND = 0; | ||
var PUSH = 1; | ||
var INC_SUB_PATH_DEPTH = 2; | ||
var PUSH_SUB_PATH = 3; | ||
// states | ||
var BEFORE_PATH = 0; | ||
var IN_PATH = 1; | ||
var BEFORE_IDENT = 2; | ||
var IN_IDENT = 3; | ||
var IN_SUB_PATH = 4; | ||
var IN_SINGLE_QUOTE = 5; | ||
var IN_DOUBLE_QUOTE = 6; | ||
var AFTER_PATH = 7; | ||
var ERROR = 8; | ||
var pathStateMachine = []; | ||
pathStateMachine[BEFORE_PATH] = { | ||
'ws': [BEFORE_PATH], | ||
'ident': [IN_IDENT, APPEND], | ||
'[': [IN_SUB_PATH], | ||
'eof': [AFTER_PATH] | ||
}; | ||
pathStateMachine[IN_PATH] = { | ||
'ws': [IN_PATH], | ||
'.': [BEFORE_IDENT], | ||
'[': [IN_SUB_PATH], | ||
'eof': [AFTER_PATH] | ||
}; | ||
pathStateMachine[BEFORE_IDENT] = { | ||
'ws': [BEFORE_IDENT], | ||
'ident': [IN_IDENT, APPEND] | ||
}; | ||
pathStateMachine[IN_IDENT] = { | ||
'ident': [IN_IDENT, APPEND], | ||
'0': [IN_IDENT, APPEND], | ||
'number': [IN_IDENT, APPEND], | ||
'ws': [IN_PATH, PUSH], | ||
'.': [BEFORE_IDENT, PUSH], | ||
'[': [IN_SUB_PATH, PUSH], | ||
'eof': [AFTER_PATH, PUSH] | ||
}; | ||
pathStateMachine[IN_SUB_PATH] = { | ||
"'": [IN_SINGLE_QUOTE, APPEND], | ||
'"': [IN_DOUBLE_QUOTE, APPEND], | ||
'[': [IN_SUB_PATH, INC_SUB_PATH_DEPTH], | ||
']': [IN_PATH, PUSH_SUB_PATH], | ||
'eof': ERROR, | ||
'else': [IN_SUB_PATH, APPEND] | ||
}; | ||
pathStateMachine[IN_SINGLE_QUOTE] = { | ||
"'": [IN_SUB_PATH, APPEND], | ||
'eof': ERROR, | ||
'else': [IN_SINGLE_QUOTE, APPEND] | ||
}; | ||
pathStateMachine[IN_DOUBLE_QUOTE] = { | ||
'"': [IN_SUB_PATH, APPEND], | ||
'eof': ERROR, | ||
'else': [IN_DOUBLE_QUOTE, APPEND] | ||
}; | ||
/** | ||
* Determine the type of a character in a keypath. | ||
* | ||
* @param {Char} ch | ||
* @return {String} type | ||
*/ | ||
function getPathCharType(ch) { | ||
if (ch === undefined) { | ||
return 'eof'; | ||
} | ||
var code = ch.charCodeAt(0); | ||
switch (code) { | ||
case 0x5B: // [ | ||
case 0x5D: // ] | ||
case 0x2E: // . | ||
case 0x22: // " | ||
case 0x27: // ' | ||
case 0x30: | ||
// 0 | ||
return ch; | ||
case 0x5F: // _ | ||
case 0x24: | ||
// $ | ||
return 'ident'; | ||
case 0x20: // Space | ||
case 0x09: // Tab | ||
case 0x0A: // Newline | ||
case 0x0D: // Return | ||
case 0xA0: // No-break space | ||
case 0xFEFF: // Byte Order Mark | ||
case 0x2028: // Line Separator | ||
case 0x2029: | ||
// Paragraph Separator | ||
return 'ws'; | ||
} | ||
// a-z, A-Z | ||
if (code >= 0x61 && code <= 0x7A || code >= 0x41 && code <= 0x5A) { | ||
return 'ident'; | ||
} | ||
// 1-9 | ||
if (code >= 0x31 && code <= 0x39) { | ||
return 'number'; | ||
} | ||
return 'else'; | ||
} | ||
/** | ||
* Format a subPath, return its plain form if it is | ||
* a literal string or number. Otherwise prepend the | ||
* dynamic indicator (*). | ||
* | ||
* @param {String} path | ||
* @return {String} | ||
*/ | ||
function formatSubPath(path) { | ||
var _exports$Vue$util = exports$2.Vue.util; | ||
var isLiteral = _exports$Vue$util.isLiteral; | ||
var stripQuotes = _exports$Vue$util.stripQuotes; | ||
var trimmed = path.trim(); | ||
// invalid leading 0 | ||
if (path.charAt(0) === '0' && isNaN(path)) { | ||
return false; | ||
} | ||
return isLiteral(trimmed) ? stripQuotes(trimmed) : '*' + trimmed; | ||
} | ||
/** | ||
* Parse a string path into an array of segments | ||
* | ||
* @param {String} path | ||
* @return {Array|undefined} | ||
*/ | ||
function parse(path) { | ||
var keys = []; | ||
var index = -1; | ||
var mode = BEFORE_PATH; | ||
var subPathDepth = 0; | ||
var c = void 0, | ||
newChar = void 0, | ||
key = void 0, | ||
type = void 0, | ||
transition = void 0, | ||
action = void 0, | ||
typeMap = void 0; | ||
var actions = []; | ||
actions[PUSH] = function () { | ||
if (key !== undefined) { | ||
keys.push(key); | ||
key = undefined; | ||
} | ||
}; | ||
actions[APPEND] = function () { | ||
if (key === undefined) { | ||
key = newChar; | ||
} else { | ||
key += newChar; | ||
} | ||
}; | ||
actions[INC_SUB_PATH_DEPTH] = function () { | ||
actions[APPEND](); | ||
subPathDepth++; | ||
}; | ||
actions[PUSH_SUB_PATH] = function () { | ||
if (subPathDepth > 0) { | ||
subPathDepth--; | ||
mode = IN_SUB_PATH; | ||
actions[APPEND](); | ||
} else { | ||
subPathDepth = 0; | ||
key = formatSubPath(key); | ||
if (key === false) { | ||
return false; | ||
} else { | ||
actions[PUSH](); | ||
} | ||
} | ||
}; | ||
function maybeUnescapeQuote() { | ||
var nextChar = path[index + 1]; | ||
if (mode === IN_SINGLE_QUOTE && nextChar === "'" || mode === IN_DOUBLE_QUOTE && nextChar === '"') { | ||
index++; | ||
newChar = '\\' + nextChar; | ||
actions[APPEND](); | ||
return true; | ||
} | ||
} | ||
while (mode != null) { | ||
index++; | ||
c = path[index]; | ||
if (c === '\\' && maybeUnescapeQuote()) { | ||
continue; | ||
} | ||
type = getPathCharType(c); | ||
typeMap = pathStateMachine[mode]; | ||
transition = typeMap[type] || typeMap['else'] || ERROR; | ||
if (transition === ERROR) { | ||
return; // parse error | ||
} | ||
mode = transition[0]; | ||
action = actions[transition[1]]; | ||
if (action) { | ||
newChar = transition[2]; | ||
newChar = newChar === undefined ? c : newChar; | ||
if (action() === false) { | ||
return; | ||
} | ||
} | ||
if (mode === AFTER_PATH) { | ||
keys.raw = path; | ||
return keys; | ||
} | ||
} | ||
} | ||
/** | ||
* External parse that check for a cache hit first | ||
* | ||
* @param {String} path | ||
* @return {Array|undefined} | ||
*/ | ||
function parsePath(path) { | ||
var hit = pathCache[path]; | ||
if (!hit) { | ||
hit = parse(path); | ||
if (hit) { | ||
pathCache[path] = hit; | ||
} | ||
} | ||
return hit; | ||
} | ||
/** | ||
* Get value from path string | ||
* | ||
* @param {Object} obj | ||
* @param {String} path | ||
* @return value | ||
*/ | ||
function getValue(obj, path) { | ||
var isObject = exports$2.Vue.util.isObject; | ||
if (!isObject(obj)) { | ||
return null; | ||
} | ||
var paths = parsePath(path); | ||
if (empty(paths)) { | ||
return null; | ||
} | ||
var ret = null; | ||
var last = obj; | ||
var length = paths.length; | ||
var i = 0; | ||
while (i < length) { | ||
var value = last[paths[i]]; | ||
if (value === undefined) { | ||
last = null; | ||
break; | ||
} | ||
last = value; | ||
i++; | ||
} | ||
ret = last; | ||
return ret; | ||
} | ||
/** | ||
* Version compare | ||
@@ -421,17 +709,51 @@ * - Inspired: | ||
function Extend (Vue) { | ||
var getPath = compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
var util = Vue.util; | ||
var _Vue$util = Vue.util; | ||
var isArray = _Vue$util.isArray; | ||
var isObject = _Vue$util.isObject; | ||
function getVal(key, lang, args) { | ||
var value = key; | ||
try { | ||
var locale = Vue.locale(lang); | ||
var val = getPath(locale, key) || locale[key]; | ||
value = (args ? format(val, args) : val) || key; | ||
} catch (e) { | ||
value = key; | ||
function parseArgs() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return value; | ||
var lang = Vue.config.lang; | ||
if (args.length === 1) { | ||
if (isObject(args[0]) || isArray(args[0])) { | ||
args = args[0]; | ||
} else if (typeof args[0] === 'string') { | ||
lang = args[0]; | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
lang = args[0]; | ||
} | ||
if (isObject(args[1]) || isArray(args[1])) { | ||
args = args[1]; | ||
} | ||
} | ||
return { lang: lang, params: args }; | ||
} | ||
function translate(locale, key, args) { | ||
if (!locale) { | ||
return null; | ||
} | ||
var val = getValue(locale, key) || locale[key]; | ||
if (!val) { | ||
return null; | ||
} | ||
return args ? format(val, args) : val; | ||
} | ||
function warnDefault(key) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
warn('Cannot translate the value of keypath "' + key + '". ' + 'Use the value of keypath as default'); | ||
} | ||
return key; | ||
} | ||
/** | ||
@@ -446,4 +768,4 @@ * Vue.t | ||
Vue.t = function (key) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
@@ -455,19 +777,8 @@ | ||
var language = Vue.config.lang; | ||
if (args.length === 1) { | ||
if (util.isObject(args[0]) || util.isArray(args[0])) { | ||
args = args[0]; | ||
} else if (typeof args[0] === 'string') { | ||
language = args[0]; | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
language = args[0]; | ||
} | ||
if (util.isObject(args[1]) || util.isArray(args[1])) { | ||
args = args[1]; | ||
} | ||
} | ||
var _parseArgs = parseArgs.apply(undefined, args); | ||
return getVal(key, language, args); | ||
var lang = _parseArgs.lang; | ||
var params = _parseArgs.params; | ||
return translate(Vue.locale(lang), key, params) || warnDefault(key); | ||
}; | ||
@@ -484,7 +795,16 @@ | ||
Vue.prototype.$t = function (key) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
if (!key) { | ||
return ''; | ||
} | ||
return Vue.t.apply(Vue, [key].concat(args)); | ||
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
var _parseArgs2 = parseArgs.apply(undefined, args); | ||
var lang = _parseArgs2.lang; | ||
var params = _parseArgs2.params; | ||
return translate(this.$options.locales && this.$options.locales[lang], key, params) || translate(Vue.locale(lang), key, params) || warnDefault(key); | ||
}; | ||
@@ -517,17 +837,8 @@ | ||
if (process.env.NODE_ENV !== 'production' && opts.lang) { | ||
warn('`options.lang` will be deprecated in vue-i18n 3.1 later.'); | ||
} | ||
var lang = opts.lang || 'en'; | ||
var lang = 'en'; | ||
if (process.env.NODE_ENV !== 'production' && opts.locales) { | ||
warn('`options.locales` will be deprecated in vue-i18n 3.1 later.'); | ||
} | ||
var locales = opts.locales || {}; | ||
exports$1.Vue = Vue; | ||
exports$2.Vue = exports$1.Vue = Vue; | ||
setupLangVM(Vue, lang); | ||
Asset(Vue); | ||
setupLocale(Vue, locales); | ||
@@ -548,12 +859,4 @@ Override(Vue, langVM); | ||
function setupLocale(Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, function (locale, lang) { | ||
Vue.locale(lang, locale); | ||
}); | ||
} | ||
} | ||
plugin.version = '3.1.0'; | ||
plugin.version = '3.0.0'; | ||
module.exports = plugin; |
/*! | ||
* vue-i18n v3.0.0 | ||
* vue-i18n v3.1.0 | ||
* (c) 2016 kazuya kawaguchi | ||
@@ -86,24 +86,2 @@ * Released under the MIT License. | ||
/** | ||
* each | ||
* | ||
* @param {Array|Object} target | ||
* @param {Function} iterator | ||
* @param {Object} [context] | ||
*/ | ||
function each(target, iterator, context) { | ||
if (Array.isArray(target)) { | ||
for (var i = 0; i < target.length; i++) { | ||
iterator.call(context || target[i], target[i], i); | ||
} | ||
} else if (exports$1.Vue.util.isPlainObject(target)) { | ||
for (var key in target) { | ||
if (hasOwn(target, key)) { | ||
iterator.call(context || target[key], target[key], key); | ||
} | ||
} | ||
} | ||
} | ||
var Watcher = void 0; | ||
@@ -152,3 +130,313 @@ /** | ||
// export default for holding the Vue reference | ||
var exports$2 = {}; | ||
// cache | ||
var pathCache = Object.create(null); | ||
// actions | ||
var APPEND = 0; | ||
var PUSH = 1; | ||
var INC_SUB_PATH_DEPTH = 2; | ||
var PUSH_SUB_PATH = 3; | ||
// states | ||
var BEFORE_PATH = 0; | ||
var IN_PATH = 1; | ||
var BEFORE_IDENT = 2; | ||
var IN_IDENT = 3; | ||
var IN_SUB_PATH = 4; | ||
var IN_SINGLE_QUOTE = 5; | ||
var IN_DOUBLE_QUOTE = 6; | ||
var AFTER_PATH = 7; | ||
var ERROR = 8; | ||
var pathStateMachine = []; | ||
pathStateMachine[BEFORE_PATH] = { | ||
'ws': [BEFORE_PATH], | ||
'ident': [IN_IDENT, APPEND], | ||
'[': [IN_SUB_PATH], | ||
'eof': [AFTER_PATH] | ||
}; | ||
pathStateMachine[IN_PATH] = { | ||
'ws': [IN_PATH], | ||
'.': [BEFORE_IDENT], | ||
'[': [IN_SUB_PATH], | ||
'eof': [AFTER_PATH] | ||
}; | ||
pathStateMachine[BEFORE_IDENT] = { | ||
'ws': [BEFORE_IDENT], | ||
'ident': [IN_IDENT, APPEND] | ||
}; | ||
pathStateMachine[IN_IDENT] = { | ||
'ident': [IN_IDENT, APPEND], | ||
'0': [IN_IDENT, APPEND], | ||
'number': [IN_IDENT, APPEND], | ||
'ws': [IN_PATH, PUSH], | ||
'.': [BEFORE_IDENT, PUSH], | ||
'[': [IN_SUB_PATH, PUSH], | ||
'eof': [AFTER_PATH, PUSH] | ||
}; | ||
pathStateMachine[IN_SUB_PATH] = { | ||
"'": [IN_SINGLE_QUOTE, APPEND], | ||
'"': [IN_DOUBLE_QUOTE, APPEND], | ||
'[': [IN_SUB_PATH, INC_SUB_PATH_DEPTH], | ||
']': [IN_PATH, PUSH_SUB_PATH], | ||
'eof': ERROR, | ||
'else': [IN_SUB_PATH, APPEND] | ||
}; | ||
pathStateMachine[IN_SINGLE_QUOTE] = { | ||
"'": [IN_SUB_PATH, APPEND], | ||
'eof': ERROR, | ||
'else': [IN_SINGLE_QUOTE, APPEND] | ||
}; | ||
pathStateMachine[IN_DOUBLE_QUOTE] = { | ||
'"': [IN_SUB_PATH, APPEND], | ||
'eof': ERROR, | ||
'else': [IN_DOUBLE_QUOTE, APPEND] | ||
}; | ||
/** | ||
* Determine the type of a character in a keypath. | ||
* | ||
* @param {Char} ch | ||
* @return {String} type | ||
*/ | ||
function getPathCharType(ch) { | ||
if (ch === undefined) { | ||
return 'eof'; | ||
} | ||
var code = ch.charCodeAt(0); | ||
switch (code) { | ||
case 0x5B: // [ | ||
case 0x5D: // ] | ||
case 0x2E: // . | ||
case 0x22: // " | ||
case 0x27: // ' | ||
case 0x30: | ||
// 0 | ||
return ch; | ||
case 0x5F: // _ | ||
case 0x24: | ||
// $ | ||
return 'ident'; | ||
case 0x20: // Space | ||
case 0x09: // Tab | ||
case 0x0A: // Newline | ||
case 0x0D: // Return | ||
case 0xA0: // No-break space | ||
case 0xFEFF: // Byte Order Mark | ||
case 0x2028: // Line Separator | ||
case 0x2029: | ||
// Paragraph Separator | ||
return 'ws'; | ||
} | ||
// a-z, A-Z | ||
if (code >= 0x61 && code <= 0x7A || code >= 0x41 && code <= 0x5A) { | ||
return 'ident'; | ||
} | ||
// 1-9 | ||
if (code >= 0x31 && code <= 0x39) { | ||
return 'number'; | ||
} | ||
return 'else'; | ||
} | ||
/** | ||
* Format a subPath, return its plain form if it is | ||
* a literal string or number. Otherwise prepend the | ||
* dynamic indicator (*). | ||
* | ||
* @param {String} path | ||
* @return {String} | ||
*/ | ||
function formatSubPath(path) { | ||
var _exports$Vue$util = exports$2.Vue.util; | ||
var isLiteral = _exports$Vue$util.isLiteral; | ||
var stripQuotes = _exports$Vue$util.stripQuotes; | ||
var trimmed = path.trim(); | ||
// invalid leading 0 | ||
if (path.charAt(0) === '0' && isNaN(path)) { | ||
return false; | ||
} | ||
return isLiteral(trimmed) ? stripQuotes(trimmed) : '*' + trimmed; | ||
} | ||
/** | ||
* Parse a string path into an array of segments | ||
* | ||
* @param {String} path | ||
* @return {Array|undefined} | ||
*/ | ||
function parse(path) { | ||
var keys = []; | ||
var index = -1; | ||
var mode = BEFORE_PATH; | ||
var subPathDepth = 0; | ||
var c = void 0, | ||
newChar = void 0, | ||
key = void 0, | ||
type = void 0, | ||
transition = void 0, | ||
action = void 0, | ||
typeMap = void 0; | ||
var actions = []; | ||
actions[PUSH] = function () { | ||
if (key !== undefined) { | ||
keys.push(key); | ||
key = undefined; | ||
} | ||
}; | ||
actions[APPEND] = function () { | ||
if (key === undefined) { | ||
key = newChar; | ||
} else { | ||
key += newChar; | ||
} | ||
}; | ||
actions[INC_SUB_PATH_DEPTH] = function () { | ||
actions[APPEND](); | ||
subPathDepth++; | ||
}; | ||
actions[PUSH_SUB_PATH] = function () { | ||
if (subPathDepth > 0) { | ||
subPathDepth--; | ||
mode = IN_SUB_PATH; | ||
actions[APPEND](); | ||
} else { | ||
subPathDepth = 0; | ||
key = formatSubPath(key); | ||
if (key === false) { | ||
return false; | ||
} else { | ||
actions[PUSH](); | ||
} | ||
} | ||
}; | ||
function maybeUnescapeQuote() { | ||
var nextChar = path[index + 1]; | ||
if (mode === IN_SINGLE_QUOTE && nextChar === "'" || mode === IN_DOUBLE_QUOTE && nextChar === '"') { | ||
index++; | ||
newChar = '\\' + nextChar; | ||
actions[APPEND](); | ||
return true; | ||
} | ||
} | ||
while (mode != null) { | ||
index++; | ||
c = path[index]; | ||
if (c === '\\' && maybeUnescapeQuote()) { | ||
continue; | ||
} | ||
type = getPathCharType(c); | ||
typeMap = pathStateMachine[mode]; | ||
transition = typeMap[type] || typeMap['else'] || ERROR; | ||
if (transition === ERROR) { | ||
return; // parse error | ||
} | ||
mode = transition[0]; | ||
action = actions[transition[1]]; | ||
if (action) { | ||
newChar = transition[2]; | ||
newChar = newChar === undefined ? c : newChar; | ||
if (action() === false) { | ||
return; | ||
} | ||
} | ||
if (mode === AFTER_PATH) { | ||
keys.raw = path; | ||
return keys; | ||
} | ||
} | ||
} | ||
/** | ||
* External parse that check for a cache hit first | ||
* | ||
* @param {String} path | ||
* @return {Array|undefined} | ||
*/ | ||
function parsePath(path) { | ||
var hit = pathCache[path]; | ||
if (!hit) { | ||
hit = parse(path); | ||
if (hit) { | ||
pathCache[path] = hit; | ||
} | ||
} | ||
return hit; | ||
} | ||
/** | ||
* Get value from path string | ||
* | ||
* @param {Object} obj | ||
* @param {String} path | ||
* @return value | ||
*/ | ||
function getValue(obj, path) { | ||
var isObject = exports$2.Vue.util.isObject; | ||
if (!isObject(obj)) { | ||
return null; | ||
} | ||
var paths = parsePath(path); | ||
if (empty(paths)) { | ||
return null; | ||
} | ||
var ret = null; | ||
var last = obj; | ||
var length = paths.length; | ||
var i = 0; | ||
while (i < length) { | ||
var value = last[paths[i]]; | ||
if (value === undefined) { | ||
last = null; | ||
break; | ||
} | ||
last = value; | ||
i++; | ||
} | ||
ret = last; | ||
return ret; | ||
} | ||
/** | ||
* Version compare | ||
@@ -425,17 +713,51 @@ * - Inspired: | ||
function Extend (Vue) { | ||
var getPath = compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
var util = Vue.util; | ||
var _Vue$util = Vue.util; | ||
var isArray = _Vue$util.isArray; | ||
var isObject = _Vue$util.isObject; | ||
function getVal(key, lang, args) { | ||
var value = key; | ||
try { | ||
var locale = Vue.locale(lang); | ||
var val = getPath(locale, key) || locale[key]; | ||
value = (args ? format(val, args) : val) || key; | ||
} catch (e) { | ||
value = key; | ||
function parseArgs() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return value; | ||
var lang = Vue.config.lang; | ||
if (args.length === 1) { | ||
if (isObject(args[0]) || isArray(args[0])) { | ||
args = args[0]; | ||
} else if (typeof args[0] === 'string') { | ||
lang = args[0]; | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
lang = args[0]; | ||
} | ||
if (isObject(args[1]) || isArray(args[1])) { | ||
args = args[1]; | ||
} | ||
} | ||
return { lang: lang, params: args }; | ||
} | ||
function translate(locale, key, args) { | ||
if (!locale) { | ||
return null; | ||
} | ||
var val = getValue(locale, key) || locale[key]; | ||
if (!val) { | ||
return null; | ||
} | ||
return args ? format(val, args) : val; | ||
} | ||
function warnDefault(key) { | ||
if ('development' !== 'production') { | ||
warn('Cannot translate the value of keypath "' + key + '". ' + 'Use the value of keypath as default'); | ||
} | ||
return key; | ||
} | ||
/** | ||
@@ -450,4 +772,4 @@ * Vue.t | ||
Vue.t = function (key) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
@@ -459,19 +781,8 @@ | ||
var language = Vue.config.lang; | ||
if (args.length === 1) { | ||
if (util.isObject(args[0]) || util.isArray(args[0])) { | ||
args = args[0]; | ||
} else if (typeof args[0] === 'string') { | ||
language = args[0]; | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
language = args[0]; | ||
} | ||
if (util.isObject(args[1]) || util.isArray(args[1])) { | ||
args = args[1]; | ||
} | ||
} | ||
var _parseArgs = parseArgs.apply(undefined, args); | ||
return getVal(key, language, args); | ||
var lang = _parseArgs.lang; | ||
var params = _parseArgs.params; | ||
return translate(Vue.locale(lang), key, params) || warnDefault(key); | ||
}; | ||
@@ -488,7 +799,16 @@ | ||
Vue.prototype.$t = function (key) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
if (!key) { | ||
return ''; | ||
} | ||
return Vue.t.apply(Vue, [key].concat(args)); | ||
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
var _parseArgs2 = parseArgs.apply(undefined, args); | ||
var lang = _parseArgs2.lang; | ||
var params = _parseArgs2.params; | ||
return translate(this.$options.locales && this.$options.locales[lang], key, params) || translate(Vue.locale(lang), key, params) || warnDefault(key); | ||
}; | ||
@@ -521,17 +841,8 @@ | ||
if ('development' !== 'production' && opts.lang) { | ||
warn('`options.lang` will be deprecated in vue-i18n 3.1 later.'); | ||
} | ||
var lang = opts.lang || 'en'; | ||
var lang = 'en'; | ||
if ('development' !== 'production' && opts.locales) { | ||
warn('`options.locales` will be deprecated in vue-i18n 3.1 later.'); | ||
} | ||
var locales = opts.locales || {}; | ||
exports$1.Vue = Vue; | ||
exports$2.Vue = exports$1.Vue = Vue; | ||
setupLangVM(Vue, lang); | ||
Asset(Vue); | ||
setupLocale(Vue, locales); | ||
@@ -552,14 +863,6 @@ Override(Vue, langVM); | ||
function setupLocale(Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, function (locale, lang) { | ||
Vue.locale(lang, locale); | ||
}); | ||
} | ||
} | ||
plugin.version = '3.1.0'; | ||
plugin.version = '3.0.0'; | ||
return plugin; | ||
})); |
/*! | ||
* vue-i18n v3.0.0 | ||
* vue-i18n v3.1.0 | ||
* (c) 2016 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.VueI18n=t()}(this,function(){"use strict";function n(n,t){window.console&&(console.warn("[vue-i18n] "+n),t&&console.warn(t.stack))}function t(n,t){return j.call(n,t)}function e(n){if(null===n||void 0===n)return!0;if(Array.isArray(n)){if(n.length>0)return!1;if(0===n.length)return!0}else if(w.Vue.util.isPlainObject(n))for(var e in n)if(t(n,e))return!1;return!0}function r(n,e,r){if(Array.isArray(n))for(var o=0;o<n.length;o++)e.call(r||n[o],n[o],o);else if(w.Vue.util.isPlainObject(n))for(var i in n)t(n,i)&&e.call(r||n[i],n[i],i)}function o(n){if(!O){var t=n.$watch("__watcher__",function(n){});O=n._watchers[0].constructor,t()}return O}function i(n){return m||(m=n._data.__ob__.dep.constructor),m}function a(n){return n&&"function"==typeof n.then}function u(n){var t=n.split("."),e=t.splice(0,2);return e.push(t.join(".")),e}function c(n,t){for(var e=u(n),r=u(t),o=0;3>o;o++){var i=parseInt(e[o]||0,10),a=parseInt(r[o]||0,10);if(i>a)return 1;if(a>i)return-1}if((e[2]+r[2]+"").indexOf("-")>-1){var c=(A.exec(e[2])||[""])[0],f=(A.exec(r[2])||[""])[0];if(""===c)return 1;if(""===f)return-1;if(c>f)return 1;if(f>c)return-1}return 0}function f(t){t.locale=function(t,e,r){return void 0===e?$[t]:void(null===e?($[t]=void 0,delete $[t]):l(t,e,function(e){e?($[t]=e,r&&r()):n("failed set `"+t+"` locale")}))}}function l(n,t,e){var r=this;"object"===("undefined"==typeof t?"undefined":_["typeof"](t))?e(t):!function(){var n=t.call(r);"function"==typeof n?n.resolved?e(n.resolved):n.requested?n.pendingCallbacks.push(e):!function(){n.requested=!0;var t=n.pendingCallbacks=[e];n(function(e){n.resolved=e;for(var r=0,o=t.length;o>r;r++)t[r](e)},function(){e()})}():a(n)&&n.then(function(n){e(n)},function(){e()})["catch"](function(n){console.error(n),e()})}()}function s(n,t){var e=n.prototype._init;n.prototype._init=function(n){var r=this;n=n||{};var o=n._parent||n.parent||this,i=o.$lang;i?this.$lang=i:this.$lang=t,this._langUnwatch=this.$lang.$watch("lang",function(n,t){p(r)}),e.call(this,n)};var r=n.prototype._destroy;n.prototype._destroy=function(){this._langUnwatch&&(this._langUnwatch(),this._langUnwatch=null),this.$lang=null,r.apply(this,arguments)}}function p(n){for(var t=n._watchers.length;t--;)n._watchers[t].update(!0)}function h(n,t){function e(n,t){var e=new r(t,n,null,{lazy:!0});return function(){return e.dirty&&e.evaluate(),a.target&&e.depend(),e.value}}var r=o(t),a=i(t);Object.defineProperty(n.config,"lang",{enumerable:!0,configurable:!0,get:e(function(){return t.lang},t),set:n.util.bind(function(n){t.lang=n},t)})}function v(n){for(var t=arguments.length,e=Array(t>1?t-1:0),r=1;t>r;r++)e[r-1]=arguments[r];return 1===e.length&&"object"===_["typeof"](e[0])&&(e=e[0]),e&&e.hasOwnProperty||(e={}),n.replace(P,function(t,r,o,i){var a=void 0;return"{"===n[i-1]&&"}"===n[i+t.length]?o:(a=e.hasOwnProperty(o)?e[o]:null,null===a||void 0===a?"":a)})}function y(n){function t(t,r,o){var i=t;try{var a=n.locale(r),u=e(a,t)||a[t];i=(o?v(u,o):u)||t}catch(c){i=t}return i}var e=-1===c("1.0.8",n.version)?n.parsers.path.getPath:n.parsers.path.get,r=n.util;return n.t=function(e){for(var o=arguments.length,i=Array(o>1?o-1:0),a=1;o>a;a++)i[a-1]=arguments[a];if(!e)return"";var u=n.config.lang;return 1===i.length?r.isObject(i[0])||r.isArray(i[0])?i=i[0]:"string"==typeof i[0]&&(u=i[0]):2===i.length&&("string"==typeof i[0]&&(u=i[0]),(r.isObject(i[1])||r.isArray(i[1]))&&(i=i[1])),t(e,u,i)},n.prototype.$t=function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),o=1;e>o;o++)r[o-1]=arguments[o];return n.t.apply(n,[t].concat(r))},n}function d(n){var t=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],e=t.lang||"en",r=t.locales||{};w.Vue=n,g(n,e),f(n),b(n,r),s(n,x),h(n,x),y(n)}function g(n,t){var e=n.config.silent;n.config.silent=!0,x||(x=new n({data:{lang:t}})),n.config.silent=e}function b(n,t){e(t)||r(t,function(t,e){n.locale(e,t)})}var _={};_["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol?"symbol":typeof n};var w={},j=Object.prototype.hasOwnProperty,O=void 0,m=void 0,A=/-([\w-.]+)/,$=Object.create(null),P=/(%|)\{([0-9a-zA-Z]+)\}/g,x=void 0;return d.version="3.0.0",d}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.VueI18n=t()}(this,function(){"use strict";function n(n,t){window.console&&(console.warn("[vue-i18n] "+n),t&&console.warn(t.stack))}function t(n,t){return j.call(n,t)}function e(n){if(null===n||void 0===n)return!0;if(Array.isArray(n)){if(n.length>0)return!1;if(0===n.length)return!0}else if(m.Vue.util.isPlainObject(n))for(var e in n)if(t(n,e))return!1;return!0}function r(n){if(!A){var t=n.$watch("__watcher__",function(n){});A=n._watchers[0].constructor,t()}return A}function o(n){return O||(O=n._data.__ob__.dep.constructor),O}function i(n){return n&&"function"==typeof n.then}function u(n){if(void 0===n)return"eof";var t=n.charCodeAt(0);switch(t){case 91:case 93:case 46:case 34:case 39:case 48:return n;case 95:case 36:return"ident";case 32:case 9:case 10:case 13:case 160:case 65279:case 8232:case 8233:return"ws"}return t>=97&&122>=t||t>=65&&90>=t?"ident":t>=49&&57>=t?"number":"else"}function a(n){var t=$.Vue.util,e=t.isLiteral,r=t.stripQuotes,o=n.trim();return"0"===n.charAt(0)&&isNaN(n)?!1:e(o)?r(o):"*"+o}function c(n){function t(){var t=n[r+1];return o===I&&"'"===t||o===L&&'"'===t?(r++,l="\\"+t,h[P](),!0):void 0}var e=[],r=-1,o=C,i=0,c=void 0,l=void 0,f=void 0,s=void 0,v=void 0,d=void 0,p=void 0,h=[];for(h[k]=function(){void 0!==f&&(e.push(f),f=void 0)},h[P]=function(){void 0===f?f=l:f+=l},h[S]=function(){h[P](),i++},h[U]=function(){if(i>0)i--,o=N,h[P]();else{if(i=0,f=a(f),f===!1)return!1;h[k]()}};null!=o;)if(r++,c=n[r],"\\"!==c||!t()){if(s=u(c),p=B[o],v=p[s]||p["else"]||Z,v===Z)return;if(o=v[0],d=h[v[1]],d&&(l=v[2],l=void 0===l?c:l,d()===!1))return;if(o===Q)return e.raw=n,e}}function l(n){var t=V[n];return t||(t=c(n),t&&(V[n]=t)),t}function f(n,t){var r=$.Vue.util.isObject;if(!r(n))return null;var o=l(t);if(e(o))return null;for(var i=null,u=n,a=o.length,c=0;a>c;){var f=u[o[c]];if(void 0===f){u=null;break}u=f,c++}return i=u}function s(t){t.locale=function(t,e,r){return void 0===e?D[t]:void(null===e?(D[t]=void 0,delete D[t]):v(t,e,function(e){e?(D[t]=e,r&&r()):n("failed set `"+t+"` locale")}))}}function v(n,t,e){var r=this;"object"===("undefined"==typeof t?"undefined":_["typeof"](t))?e(t):!function(){var n=t.call(r);"function"==typeof n?n.resolved?e(n.resolved):n.requested?n.pendingCallbacks.push(e):!function(){n.requested=!0;var t=n.pendingCallbacks=[e];n(function(e){n.resolved=e;for(var r=0,o=t.length;o>r;r++)t[r](e)},function(){e()})}():i(n)&&n.then(function(n){e(n)},function(){e()})["catch"](function(n){console.error(n),e()})}()}function d(n,t){var e=n.prototype._init;n.prototype._init=function(n){var r=this;n=n||{};var o=n._parent||n.parent||this,i=o.$lang;i?this.$lang=i:this.$lang=t,this._langUnwatch=this.$lang.$watch("lang",function(n,t){p(r)}),e.call(this,n)};var r=n.prototype._destroy;n.prototype._destroy=function(){this._langUnwatch&&(this._langUnwatch(),this._langUnwatch=null),this.$lang=null,r.apply(this,arguments)}}function p(n){for(var t=n._watchers.length;t--;)n._watchers[t].update(!0)}function h(n,t){function e(n,t){var e=new i(t,n,null,{lazy:!0});return function(){return e.dirty&&e.evaluate(),u.target&&e.depend(),e.value}}var i=r(t),u=o(t);Object.defineProperty(n.config,"lang",{enumerable:!0,configurable:!0,get:e(function(){return t.lang},t),set:n.util.bind(function(n){t.lang=n},t)})}function y(n){for(var t=arguments.length,e=Array(t>1?t-1:0),r=1;t>r;r++)e[r-1]=arguments[r];return 1===e.length&&"object"===_["typeof"](e[0])&&(e=e[0]),e&&e.hasOwnProperty||(e={}),n.replace(E,function(t,r,o,i){var u=void 0;return"{"===n[i-1]&&"}"===n[i+t.length]?o:(u=e.hasOwnProperty(o)?e[o]:null,null===u||void 0===u?"":u)})}function g(n){function t(){for(var t=arguments.length,e=Array(t),r=0;t>r;r++)e[r]=arguments[r];var o=n.config.lang;return 1===e.length?u(e[0])||i(e[0])?e=e[0]:"string"==typeof e[0]&&(o=e[0]):2===e.length&&("string"==typeof e[0]&&(o=e[0]),(u(e[1])||i(e[1]))&&(e=e[1])),{lang:o,params:e}}function e(n,t,e){if(!n)return null;var r=f(n,t)||n[t];return r?e?y(r,e):r:null}function r(n){return n}var o=n.util,i=o.isArray,u=o.isObject;return n.t=function(o){for(var i=arguments.length,u=Array(i>1?i-1:0),a=1;i>a;a++)u[a-1]=arguments[a];if(!o)return"";var c=t.apply(void 0,u),l=c.lang,f=c.params;return e(n.locale(l),o,f)||r(o)},n.prototype.$t=function(o){if(!o)return"";for(var i=arguments.length,u=Array(i>1?i-1:0),a=1;i>a;a++)u[a-1]=arguments[a];var c=t.apply(void 0,u),l=c.lang,f=c.params;return e(this.$options.locales&&this.$options.locales[l],o,f)||e(n.locale(l),o,f)||r(o)},n}function w(n){var t=(arguments.length<=1||void 0===arguments[1]?{}:arguments[1],"en");$.Vue=m.Vue=n,b(n,t),s(n),d(n,F),h(n,F),g(n)}function b(n,t){var e=n.config.silent;n.config.silent=!0,F||(F=new n({data:{lang:t}})),n.config.silent=e}var _={};_["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol?"symbol":typeof n};var m={},j=Object.prototype.hasOwnProperty,A=void 0,O=void 0,$={},V=Object.create(null),P=0,k=1,S=2,U=3,C=0,q=1,x=2,z=3,N=4,I=5,L=6,Q=7,Z=8,B=[];B[C]={ws:[C],ident:[z,P],"[":[N],eof:[Q]},B[q]={ws:[q],".":[x],"[":[N],eof:[Q]},B[x]={ws:[x],ident:[z,P]},B[z]={ident:[z,P],0:[z,P],number:[z,P],ws:[q,k],".":[x,k],"[":[N,k],eof:[Q,k]},B[N]={"'":[I,P],'"':[L,P],"[":[N,S],"]":[q,U],eof:Z,"else":[N,P]},B[I]={"'":[N,P],eof:Z,"else":[I,P]},B[L]={'"':[N,P],eof:Z,"else":[L,P]};var D=Object.create(null),E=/(%|)\{([0-9a-zA-Z]+)\}/g,F=void 0;return w.version="3.1.0",w}); |
{ | ||
"name": "vue-i18n", | ||
"description": "Internationalization plugin for Vue.js", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"author": { | ||
@@ -12,7 +12,2 @@ "name": "kazuya kawaguchi", | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
}, | ||
"devDependencies": { | ||
@@ -26,7 +21,10 @@ "babel-core": "^6.7.4", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"eslint": "^2.5.1", | ||
"eslint-config-standard": "^5.1.0", | ||
"conventional-changelog-cli": "^1.1.1", | ||
"conventional-github-releaser": "^1.1.2", | ||
"eslint": "^2.8.0", | ||
"eslint-config-standard": "^5.2.0", | ||
"eslint-loader": "^1.3.0", | ||
"eslint-plugin-promise": "^1.1.0", | ||
"eslint-plugin-standard": "^1.3.2", | ||
"git-commit-message-convention": "git://github.com/kazupon/git-commit-message-convention.git", | ||
"istanbul-instrumenter-loader": "^0.2.0", | ||
@@ -84,2 +82,3 @@ "json-loader": "^0.5.4", | ||
"build": "node config/build.js", | ||
"changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js", | ||
"ci": "npm run lint && npm run coverage && npm run coveralls && npm run sauce", | ||
@@ -96,2 +95,3 @@ "clean": "rm -rf coverage && rm -rf dist/*.*", | ||
"mobile": "VUE_I18N_TYPE=sauce SAUCE=batch3 karma start config/karma.conf.js", | ||
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js", | ||
"sauce": "npm run coolkids && npm run ie && npm run mobile", | ||
@@ -98,0 +98,0 @@ "test": "npm run ci", |
@@ -18,4 +18,11 @@ # vue-i18n | ||
## npm | ||
## CDN | ||
jsdelivr | ||
```html | ||
<script src="https://cdn.jsdelivr.net/vue.i18n/3.1.0/vue-i18n.min.js"></script> | ||
``` | ||
## NPM | ||
```shell | ||
@@ -27,2 +34,4 @@ $ npm install vue-i18n | ||
> :warning: if you are using `vue-router`, you must install with `Vue.use()` in advance of instance methods (`router#map`, `router#start`, ...etc). | ||
```javascript | ||
@@ -201,2 +210,64 @@ var Vue = require('vue') | ||
# Component locale | ||
You can translate component based. | ||
The below locale setting example: | ||
```javascript | ||
var locales = { | ||
en: { | ||
message: { | ||
hello: 'hello world' | ||
} | ||
}, | ||
ja: { | ||
message: { | ||
hello: 'こんにちは、世界' | ||
} | ||
} | ||
} | ||
Vue.config.lang = 'ja' | ||
Object.keys(locales).forEach(function (lang) { | ||
Vue.locale(lang, locales[lang]) | ||
}) | ||
new Vue({ | ||
el: '#app', | ||
components: { | ||
component1: { | ||
template: '<p>component1 local: {{ $t("hello") }}</p>' | ||
+ '<p>component1 global: {{ $t("message.hello") }}</p>', | ||
locales: { | ||
en: { hello: 'hello component1' }, | ||
ja: { hello: 'こんにちは、component1' } | ||
} | ||
} | ||
} | ||
}) | ||
``` | ||
Template the following: | ||
```html | ||
<div id="app"> | ||
<p>{{ $t("message.hello") }}</p> | ||
<component1></component1> | ||
</div> | ||
``` | ||
Output the following: | ||
```html | ||
<div id="app"> | ||
<p>こんにちは、世界</p> | ||
<p>component1 local: こんにちは、component1</p> | ||
<p>component1 global: こんにちは、世界</p> | ||
</div> | ||
``` | ||
> :pencil: If you set the locale of same keypath as global locale (`Vue.locale()`), in its component, `$t` is translate with component locale. | ||
# Dynamic locale | ||
@@ -314,4 +385,17 @@ | ||
- **Usage:** | ||
This is the same as the `$t` method. This is translate function for global. more detail see [$t](https://github.com/kazupon/vue-i18n#$t) | ||
This is the same as the `$t` method. This is translate function for global locale only. more detail see [$t](https://github.com/kazupon/vue-i18n#$t) | ||
## Constructor Options | ||
### locales | ||
- **Type:** `Object` | ||
- **Details:** | ||
A locale definition object to be made available to the Vue instance only. | ||
- **See also:** | ||
- [$t](https://github.com/kazupon/vue-i18n#$t) | ||
## Instance Methods | ||
@@ -330,3 +414,3 @@ | ||
- **Usage:** | ||
Translate the locale of `keypath`. If you specified `lang`, translate the locale of `lang`. If you specified `keypath` of list / named formatting local, you must specify `arguments` too. For `arguments` more details see [Formatting](https://github.com/kazupon/vue-i18n#formatting). | ||
Translate the locale of `keypath`. Translate in preferentially component locale than global locale. If not specified component locale, translate with global locale. If you specified `lang`, translate the locale of `lang`. If you specified `keypath` of list / named formatting local, you must specify `arguments` too. For `arguments` more details see [Formatting](https://github.com/kazupon/vue-i18n#formatting). | ||
@@ -333,0 +417,0 @@ |
@@ -0,3 +1,4 @@ | ||
import { warn } from './util' | ||
import format from './format' | ||
import compare from './compare' | ||
import { getValue } from './path' | ||
@@ -13,20 +14,42 @@ | ||
export default function (Vue) { | ||
const getPath = (compare('1.0.8', Vue.version) === -1) | ||
? Vue.parsers.path.getPath | ||
: Vue.parsers.path.get | ||
const util = Vue.util | ||
const { isArray, isObject } = Vue.util | ||
function getVal (key, lang, args) { | ||
let value = key | ||
try { | ||
let locale = Vue.locale(lang) | ||
let val = getPath(locale, key) || locale[key] | ||
value = (args ? format(val, args) : val) || key | ||
} catch (e) { | ||
value = key | ||
function parseArgs (...args) { | ||
let lang = Vue.config.lang | ||
if (args.length === 1) { | ||
if (isObject(args[0]) || isArray(args[0])) { | ||
args = args[0] | ||
} else if (typeof args[0] === 'string') { | ||
lang = args[0] | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
lang = args[0] | ||
} | ||
if (isObject(args[1]) || isArray(args[1])) { | ||
args = args[1] | ||
} | ||
} | ||
return value | ||
return { lang, params: args } | ||
} | ||
function translate (locale, key, args) { | ||
if (!locale) { return null } | ||
let val = getValue(locale, key) || locale[key] | ||
if (!val) { return null } | ||
return args ? format(val, args) : val | ||
} | ||
function warnDefault (key) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
warn('Cannot translate the value of keypath "' + key + '". ' | ||
+ 'Use the value of keypath as default') | ||
} | ||
return key | ||
} | ||
/** | ||
@@ -43,19 +66,4 @@ * Vue.t | ||
let language = Vue.config.lang | ||
if (args.length === 1) { | ||
if (util.isObject(args[0]) || util.isArray(args[0])) { | ||
args = args[0] | ||
} else if (typeof args[0] === 'string') { | ||
language = args[0] | ||
} | ||
} else if (args.length === 2) { | ||
if (typeof args[0] === 'string') { | ||
language = args[0] | ||
} | ||
if (util.isObject(args[1]) || util.isArray(args[1])) { | ||
args = args[1] | ||
} | ||
} | ||
return getVal(key, language, args) | ||
const { lang, params } = parseArgs(...args) | ||
return translate(Vue.locale(lang), key, params) || warnDefault(key) | ||
} | ||
@@ -72,4 +80,9 @@ | ||
Vue.prototype.$t = (key, ...args) => { | ||
return Vue.t(key, ...args) | ||
Vue.prototype.$t = function (key, ...args) { | ||
if (!key) { return '' } | ||
const { lang, params } = parseArgs(...args) | ||
return translate(this.$options.locales && this.$options.locales[lang], key, params) | ||
|| translate(Vue.locale(lang), key, params) | ||
|| warnDefault(key) | ||
} | ||
@@ -76,0 +89,0 @@ |
@@ -1,2 +0,3 @@ | ||
import util, { warn, empty, each } from './util' | ||
import util, { warn } from './util' | ||
import path from './path' | ||
import compare from './compare' | ||
@@ -32,17 +33,8 @@ import Asset from './asset' | ||
if (process.env.NODE_ENV !== 'production' && opts.lang) { | ||
warn('`options.lang` will be deprecated in vue-i18n 3.1 later.') | ||
} | ||
let lang = opts.lang || 'en' | ||
let lang = 'en' | ||
if (process.env.NODE_ENV !== 'production' && opts.locales) { | ||
warn('`options.locales` will be deprecated in vue-i18n 3.1 later.') | ||
} | ||
let locales = opts.locales || {} | ||
util.Vue = Vue | ||
path.Vue = util.Vue = Vue | ||
setupLangVM(Vue, lang) | ||
Asset(Vue) | ||
setupLocale(Vue, locales) | ||
@@ -63,12 +55,4 @@ Override(Vue, langVM) | ||
function setupLocale (Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, (locale, lang) => { | ||
Vue.locale(lang, locale) | ||
}) | ||
} | ||
} | ||
plugin.version = '3.1.0' | ||
plugin.version = '3.0.0' | ||
export default plugin |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
75406
17
2116
482
3
0
42