Comparing version 2.4.1 to 3.0.0
@@ -0,1 +1,15 @@ | ||
<a name="3.0.0"></a> | ||
# [3.0.0](https://github.com/kazupon/vue-i18n/compare/v2.4.1...v3.0.0) (2016-04-18) | ||
### Features | ||
* **lang:** support lang reactive changing ([203ee85](https://github.com/kazupon/vue-i18n/commit/203ee85)), closes [#2](https://github.com/kazupon/vue-i18n/issues/2) [#15](https://github.com/kazupon/vue-i18n/issues/15) | ||
* **locale:** support dynamic local ([4d61e8d](https://github.com/kazupon/vue-i18n/commit/4d61e8d)), closes [#6](https://github.com/kazupon/vue-i18n/issues/6) [#21](https://github.com/kazupon/vue-i18n/issues/21) | ||
### DEPRECATED | ||
* **index:** plugin install `Vue.use` options (`options.locales`, `options.lang`). See [README](https://github.com/kazupon/vue-i18n/blob/dev/README.md) | ||
<a name="2.4.1"></a> | ||
@@ -2,0 +16,0 @@ ## [2.4.1](https://github.com/kazupon/vue-i18n/compare/v2.4.0...v2.4.1) (2016-02-29) |
/*! | ||
* vue-i18n v2.4.1 | ||
* vue-i18n v3.0.0 | ||
* (c) 2016 kazuya kawaguchi | ||
@@ -9,54 +9,141 @@ * Released under the MIT License. | ||
var babelHelpers = {}; | ||
babelHelpers.typeof = function (obj) { | ||
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
}; | ||
babelHelpers; | ||
babelHelpers; | ||
/** | ||
* String format template | ||
* - Inspired: | ||
* https://github.com/Matt-Esch/string-template/index.js | ||
* Utilties | ||
*/ | ||
var RE_NARGS = /(%|)\{([0-9a-zA-Z]+)\}/g; | ||
// export default for holding the Vue reference | ||
var exports$1 = {}; | ||
/** | ||
* template | ||
* | ||
* @param {String} string | ||
* @param {Array} ...args | ||
* @return {String} | ||
* warn | ||
* | ||
* @param {String} msg | ||
* @param {Error} [err] | ||
* | ||
*/ | ||
function format (string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
function warn(msg, err) { | ||
if (window.console) { | ||
console.warn('[vue-i18n] ' + msg); | ||
if (err) { | ||
console.warn(err.stack); | ||
} | ||
} | ||
} | ||
if (args.length === 1 && babelHelpers.typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
/** | ||
* Check whether the object has the property. | ||
* | ||
* @param {Object} obj | ||
* @param {String} key | ||
* @return {Boolean} | ||
*/ | ||
function hasOwn(obj, key) { | ||
return hasOwnProperty.call(obj, key); | ||
} | ||
/** | ||
* empty | ||
* | ||
* @param {Array|Object} target | ||
* @return {Boolean} | ||
*/ | ||
function empty(target) { | ||
if (target === null || target === undefined) { | ||
return true; | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
if (Array.isArray(target)) { | ||
if (target.length > 0) { | ||
return false; | ||
} | ||
if (target.length === 0) { | ||
return true; | ||
} | ||
} else if (exports$1.Vue.util.isPlainObject(target)) { | ||
for (var key in target) { | ||
if (hasOwn(target, key)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return string.replace(RE_NARGS, function (match, prefix, i, index) { | ||
var result = undefined; | ||
return true; | ||
} | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null; | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
/** | ||
* each | ||
* | ||
* @param {Array|Object} target | ||
* @param {Function} iterator | ||
* @param {Object} [context] | ||
*/ | ||
return result; | ||
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; | ||
/** | ||
* getWatcher | ||
* | ||
* @param {Vue} vm | ||
* @return {Watcher} | ||
*/ | ||
function getWatcher(vm) { | ||
if (!Watcher) { | ||
var unwatch = vm.$watch('__watcher__', function (a) {}); | ||
Watcher = vm._watchers[0].constructor; | ||
unwatch(); | ||
} | ||
return Watcher; | ||
} | ||
var Dep = void 0; | ||
/** | ||
* getDep | ||
* | ||
* @param {Vue} vm | ||
* @return {Dep} | ||
*/ | ||
function getDep(vm) { | ||
if (!Dep) { | ||
Dep = vm._data.__ob__.dep.constructor; | ||
} | ||
return Dep; | ||
} | ||
/** | ||
* Forgiving check for a promise | ||
* | ||
* @param {Object} p | ||
* @return {Boolean} | ||
*/ | ||
function isPromise(p) { | ||
return p && typeof p.then === 'function'; | ||
} | ||
/** | ||
* Version compare | ||
@@ -121,12 +208,215 @@ * - Inspired: | ||
var locales = Object.create(null); // locales store | ||
function Asset (Vue) { | ||
/** | ||
* Register or retrieve a global locale definition. | ||
* | ||
* @param {String} id | ||
* @param {Object | Function | Promise} definition | ||
* @param {Function} cb | ||
*/ | ||
Vue.locale = function (id, definition, cb) { | ||
if (definition === undefined) { | ||
// gettter | ||
return locales[id]; | ||
} else { | ||
// setter | ||
if (definition === null) { | ||
locales[id] = undefined; | ||
delete locales[id]; | ||
} else { | ||
setLocale(id, definition, function (locale) { | ||
if (locale) { | ||
locales[id] = locale; | ||
cb && cb(); | ||
} else { | ||
warn('failed set `' + id + '` locale'); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
function setLocale(id, definition, cb) { | ||
var _this = this; | ||
if ((typeof definition === 'undefined' ? 'undefined' : babelHelpers.typeof(definition)) === 'object') { | ||
// sync | ||
cb(definition); | ||
} else { | ||
(function () { | ||
var future = definition.call(_this); | ||
if (typeof future === 'function') { | ||
if (future.resolved) { | ||
// cached | ||
cb(future.resolved); | ||
} else if (future.requested) { | ||
// pool callbacks | ||
future.pendingCallbacks.push(cb); | ||
} else { | ||
(function () { | ||
future.requested = true; | ||
var cbs = future.pendingCallbacks = [cb]; | ||
future(function (locale) { | ||
// resolve | ||
future.resolved = locale; | ||
for (var i = 0, l = cbs.length; i < l; i++) { | ||
cbs[i](locale); | ||
} | ||
}, function () { | ||
// reject | ||
cb(); | ||
}); | ||
})(); | ||
} | ||
} else if (isPromise(future)) { | ||
// promise | ||
future.then(function (locale) { | ||
// resolve | ||
cb(locale); | ||
}, function () { | ||
// reject | ||
cb(); | ||
}).catch(function (err) { | ||
console.error(err); | ||
cb(); | ||
}); | ||
} | ||
})(); | ||
} | ||
} | ||
function Override (Vue, langVM) { | ||
// override _init | ||
var init = Vue.prototype._init; | ||
Vue.prototype._init = function (options) { | ||
var _this = this; | ||
options = options || {}; | ||
var root = options._parent || options.parent || this; | ||
var lang = root.$lang; | ||
if (lang) { | ||
this.$lang = lang; | ||
} else { | ||
this.$lang = langVM; | ||
} | ||
this._langUnwatch = this.$lang.$watch('lang', function (a, b) { | ||
update(_this); | ||
}); | ||
init.call(this, options); | ||
}; | ||
// override _destroy | ||
var destroy = Vue.prototype._destroy; | ||
Vue.prototype._destroy = function () { | ||
if (this._langUnwatch) { | ||
this._langUnwatch(); | ||
this._langUnwatch = null; | ||
} | ||
this.$lang = null; | ||
destroy.apply(this, arguments); | ||
}; | ||
} | ||
function update(vm) { | ||
var i = vm._watchers.length; | ||
while (i--) { | ||
vm._watchers[i].update(true); // shallow updates | ||
} | ||
} | ||
function Config (Vue, langVM) { | ||
var Watcher = getWatcher(langVM); | ||
var Dep = getDep(langVM); | ||
function makeComputedGetter(getter, owner) { | ||
var watcher = new Watcher(owner, getter, null, { | ||
lazy: true | ||
}); | ||
return function computedGetter() { | ||
if (watcher.dirty) { | ||
watcher.evaluate(); | ||
} | ||
if (Dep.target) { | ||
watcher.depend(); | ||
} | ||
return watcher.value; | ||
}; | ||
} | ||
// define Vue.config.lang configration | ||
Object.defineProperty(Vue.config, 'lang', { | ||
enumerable: true, | ||
configurable: true, | ||
get: makeComputedGetter(function () { | ||
return langVM.lang; | ||
}, langVM), | ||
set: Vue.util.bind(function (val) { | ||
langVM.lang = val; | ||
}, langVM) | ||
}); | ||
} | ||
/** | ||
* String format template | ||
* - Inspired: | ||
* https://github.com/Matt-Esch/string-template/index.js | ||
*/ | ||
var RE_NARGS = /(%|)\{([0-9a-zA-Z]+)\}/g; | ||
/** | ||
* template | ||
* | ||
* @param {String} string | ||
* @param {Array} ...args | ||
* @return {String} | ||
*/ | ||
function format (string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (args.length === 1 && babelHelpers.typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
} | ||
return string.replace(RE_NARGS, function (match, prefix, i, index) { | ||
var result = void 0; | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null; | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
return result; | ||
} | ||
}); | ||
} | ||
/** | ||
* extend | ||
* | ||
* @param {Vue} Vue | ||
* @param {Object} locales | ||
* @return {Vue} | ||
*/ | ||
function extend (Vue, locales) { | ||
var getPath = Vue.version && compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
function Extend (Vue) { | ||
var getPath = compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
var util = Vue.util; | ||
@@ -137,3 +427,4 @@ | ||
try { | ||
var val = getPath(locales[lang], key) || locales[lang][key]; | ||
var locale = Vue.locale(lang); | ||
var val = getPath(locale, key) || locale[key]; | ||
value = (args ? format(val, args) : val) || key; | ||
@@ -201,2 +492,4 @@ } catch (e) { | ||
var langVM = void 0; // singleton | ||
/** | ||
@@ -210,31 +503,54 @@ * plugin | ||
function plugin(Vue) { | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? { lang: 'en', locales: {} } : arguments[1]; | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
defineConfig(Vue.config, opts.lang); | ||
extend(Vue, opts.locales); | ||
if (process.env.NODE_ENV !== 'production' && plugin.installed) { | ||
warn('already installed.'); | ||
return; | ||
} | ||
if (process.env.NODE_ENV !== 'production' && (!Vue.version || compare(Vue.version, '1.0') < 0)) { | ||
warn('vue-i18n (' + plugin.version + ') need to use vue version 1.0 or later (vue version: ' + Vue.version + ').'); | ||
return; | ||
} | ||
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'; | ||
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; | ||
setupLangVM(Vue, lang); | ||
Asset(Vue); | ||
setupLocale(Vue, locales); | ||
Override(Vue, langVM); | ||
Config(Vue, langVM); | ||
Extend(Vue); | ||
} | ||
/** | ||
* defineConfig | ||
* | ||
* This function define `lang` property to `Vue.config`. | ||
* | ||
* @param {Object} config | ||
* @param {String} lang | ||
* @private | ||
*/ | ||
function setupLangVM(Vue, lang) { | ||
var silent = Vue.config.silent; | ||
Vue.config.silent = true; | ||
if (!langVM) { | ||
langVM = new Vue({ data: { lang: lang } }); | ||
} | ||
Vue.config.silent = silent; | ||
} | ||
function defineConfig(config, lang) { | ||
Object.defineProperty(config, 'lang', { | ||
get: function get() { | ||
return lang; | ||
}, | ||
set: function set(val) { | ||
lang = val; | ||
} | ||
}); | ||
function setupLocale(Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, function (locale, lang) { | ||
Vue.locale(lang, locale); | ||
}); | ||
} | ||
} | ||
plugin.version = '2.4.1'; | ||
plugin.version = '3.0.0'; | ||
module.exports = plugin; |
/*! | ||
* vue-i18n v2.4.1 | ||
* vue-i18n v3.0.0 | ||
* (c) 2016 kazuya kawaguchi | ||
@@ -9,58 +9,145 @@ * Released under the MIT License. | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
global.VueI18n = factory(); | ||
(global.VueI18n = factory()); | ||
}(this, function () { 'use strict'; | ||
var babelHelpers = {}; | ||
babelHelpers.typeof = function (obj) { | ||
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
}; | ||
babelHelpers; | ||
babelHelpers; | ||
/** | ||
* String format template | ||
* - Inspired: | ||
* https://github.com/Matt-Esch/string-template/index.js | ||
* Utilties | ||
*/ | ||
var RE_NARGS = /(%|)\{([0-9a-zA-Z]+)\}/g; | ||
// export default for holding the Vue reference | ||
var exports$1 = {}; | ||
/** | ||
* template | ||
* | ||
* @param {String} string | ||
* @param {Array} ...args | ||
* @return {String} | ||
* warn | ||
* | ||
* @param {String} msg | ||
* @param {Error} [err] | ||
* | ||
*/ | ||
function format (string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
function warn(msg, err) { | ||
if (window.console) { | ||
console.warn('[vue-i18n] ' + msg); | ||
if (err) { | ||
console.warn(err.stack); | ||
} | ||
} | ||
} | ||
if (args.length === 1 && babelHelpers.typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
/** | ||
* Check whether the object has the property. | ||
* | ||
* @param {Object} obj | ||
* @param {String} key | ||
* @return {Boolean} | ||
*/ | ||
function hasOwn(obj, key) { | ||
return hasOwnProperty.call(obj, key); | ||
} | ||
/** | ||
* empty | ||
* | ||
* @param {Array|Object} target | ||
* @return {Boolean} | ||
*/ | ||
function empty(target) { | ||
if (target === null || target === undefined) { | ||
return true; | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
if (Array.isArray(target)) { | ||
if (target.length > 0) { | ||
return false; | ||
} | ||
if (target.length === 0) { | ||
return true; | ||
} | ||
} else if (exports$1.Vue.util.isPlainObject(target)) { | ||
for (var key in target) { | ||
if (hasOwn(target, key)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return string.replace(RE_NARGS, function (match, prefix, i, index) { | ||
var result = undefined; | ||
return true; | ||
} | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null; | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
/** | ||
* each | ||
* | ||
* @param {Array|Object} target | ||
* @param {Function} iterator | ||
* @param {Object} [context] | ||
*/ | ||
return result; | ||
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; | ||
/** | ||
* getWatcher | ||
* | ||
* @param {Vue} vm | ||
* @return {Watcher} | ||
*/ | ||
function getWatcher(vm) { | ||
if (!Watcher) { | ||
var unwatch = vm.$watch('__watcher__', function (a) {}); | ||
Watcher = vm._watchers[0].constructor; | ||
unwatch(); | ||
} | ||
return Watcher; | ||
} | ||
var Dep = void 0; | ||
/** | ||
* getDep | ||
* | ||
* @param {Vue} vm | ||
* @return {Dep} | ||
*/ | ||
function getDep(vm) { | ||
if (!Dep) { | ||
Dep = vm._data.__ob__.dep.constructor; | ||
} | ||
return Dep; | ||
} | ||
/** | ||
* Forgiving check for a promise | ||
* | ||
* @param {Object} p | ||
* @return {Boolean} | ||
*/ | ||
function isPromise(p) { | ||
return p && typeof p.then === 'function'; | ||
} | ||
/** | ||
* Version compare | ||
@@ -125,12 +212,215 @@ * - Inspired: | ||
var locales = Object.create(null); // locales store | ||
function Asset (Vue) { | ||
/** | ||
* Register or retrieve a global locale definition. | ||
* | ||
* @param {String} id | ||
* @param {Object | Function | Promise} definition | ||
* @param {Function} cb | ||
*/ | ||
Vue.locale = function (id, definition, cb) { | ||
if (definition === undefined) { | ||
// gettter | ||
return locales[id]; | ||
} else { | ||
// setter | ||
if (definition === null) { | ||
locales[id] = undefined; | ||
delete locales[id]; | ||
} else { | ||
setLocale(id, definition, function (locale) { | ||
if (locale) { | ||
locales[id] = locale; | ||
cb && cb(); | ||
} else { | ||
warn('failed set `' + id + '` locale'); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
function setLocale(id, definition, cb) { | ||
var _this = this; | ||
if ((typeof definition === 'undefined' ? 'undefined' : babelHelpers.typeof(definition)) === 'object') { | ||
// sync | ||
cb(definition); | ||
} else { | ||
(function () { | ||
var future = definition.call(_this); | ||
if (typeof future === 'function') { | ||
if (future.resolved) { | ||
// cached | ||
cb(future.resolved); | ||
} else if (future.requested) { | ||
// pool callbacks | ||
future.pendingCallbacks.push(cb); | ||
} else { | ||
(function () { | ||
future.requested = true; | ||
var cbs = future.pendingCallbacks = [cb]; | ||
future(function (locale) { | ||
// resolve | ||
future.resolved = locale; | ||
for (var i = 0, l = cbs.length; i < l; i++) { | ||
cbs[i](locale); | ||
} | ||
}, function () { | ||
// reject | ||
cb(); | ||
}); | ||
})(); | ||
} | ||
} else if (isPromise(future)) { | ||
// promise | ||
future.then(function (locale) { | ||
// resolve | ||
cb(locale); | ||
}, function () { | ||
// reject | ||
cb(); | ||
}).catch(function (err) { | ||
console.error(err); | ||
cb(); | ||
}); | ||
} | ||
})(); | ||
} | ||
} | ||
function Override (Vue, langVM) { | ||
// override _init | ||
var init = Vue.prototype._init; | ||
Vue.prototype._init = function (options) { | ||
var _this = this; | ||
options = options || {}; | ||
var root = options._parent || options.parent || this; | ||
var lang = root.$lang; | ||
if (lang) { | ||
this.$lang = lang; | ||
} else { | ||
this.$lang = langVM; | ||
} | ||
this._langUnwatch = this.$lang.$watch('lang', function (a, b) { | ||
update(_this); | ||
}); | ||
init.call(this, options); | ||
}; | ||
// override _destroy | ||
var destroy = Vue.prototype._destroy; | ||
Vue.prototype._destroy = function () { | ||
if (this._langUnwatch) { | ||
this._langUnwatch(); | ||
this._langUnwatch = null; | ||
} | ||
this.$lang = null; | ||
destroy.apply(this, arguments); | ||
}; | ||
} | ||
function update(vm) { | ||
var i = vm._watchers.length; | ||
while (i--) { | ||
vm._watchers[i].update(true); // shallow updates | ||
} | ||
} | ||
function Config (Vue, langVM) { | ||
var Watcher = getWatcher(langVM); | ||
var Dep = getDep(langVM); | ||
function makeComputedGetter(getter, owner) { | ||
var watcher = new Watcher(owner, getter, null, { | ||
lazy: true | ||
}); | ||
return function computedGetter() { | ||
if (watcher.dirty) { | ||
watcher.evaluate(); | ||
} | ||
if (Dep.target) { | ||
watcher.depend(); | ||
} | ||
return watcher.value; | ||
}; | ||
} | ||
// define Vue.config.lang configration | ||
Object.defineProperty(Vue.config, 'lang', { | ||
enumerable: true, | ||
configurable: true, | ||
get: makeComputedGetter(function () { | ||
return langVM.lang; | ||
}, langVM), | ||
set: Vue.util.bind(function (val) { | ||
langVM.lang = val; | ||
}, langVM) | ||
}); | ||
} | ||
/** | ||
* String format template | ||
* - Inspired: | ||
* https://github.com/Matt-Esch/string-template/index.js | ||
*/ | ||
var RE_NARGS = /(%|)\{([0-9a-zA-Z]+)\}/g; | ||
/** | ||
* template | ||
* | ||
* @param {String} string | ||
* @param {Array} ...args | ||
* @return {String} | ||
*/ | ||
function format (string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (args.length === 1 && babelHelpers.typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
} | ||
return string.replace(RE_NARGS, function (match, prefix, i, index) { | ||
var result = void 0; | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
} else { | ||
result = args.hasOwnProperty(i) ? args[i] : null; | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
return result; | ||
} | ||
}); | ||
} | ||
/** | ||
* extend | ||
* | ||
* @param {Vue} Vue | ||
* @param {Object} locales | ||
* @return {Vue} | ||
*/ | ||
function extend (Vue, locales) { | ||
var getPath = Vue.version && compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
function Extend (Vue) { | ||
var getPath = compare('1.0.8', Vue.version) === -1 ? Vue.parsers.path.getPath : Vue.parsers.path.get; | ||
var util = Vue.util; | ||
@@ -141,3 +431,4 @@ | ||
try { | ||
var val = getPath(locales[lang], key) || locales[lang][key]; | ||
var locale = Vue.locale(lang); | ||
var val = getPath(locale, key) || locale[key]; | ||
value = (args ? format(val, args) : val) || key; | ||
@@ -205,2 +496,4 @@ } catch (e) { | ||
var langVM = void 0; // singleton | ||
/** | ||
@@ -214,30 +507,53 @@ * plugin | ||
function plugin(Vue) { | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? { lang: 'en', locales: {} } : arguments[1]; | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
defineConfig(Vue.config, opts.lang); | ||
extend(Vue, opts.locales); | ||
if ('development' !== 'production' && plugin.installed) { | ||
warn('already installed.'); | ||
return; | ||
} | ||
if ('development' !== 'production' && (!Vue.version || compare(Vue.version, '1.0') < 0)) { | ||
warn('vue-i18n (' + plugin.version + ') need to use vue version 1.0 or later (vue version: ' + Vue.version + ').'); | ||
return; | ||
} | ||
if ('development' !== 'production' && opts.lang) { | ||
warn('`options.lang` will be deprecated in vue-i18n 3.1 later.'); | ||
} | ||
var lang = opts.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; | ||
setupLangVM(Vue, lang); | ||
Asset(Vue); | ||
setupLocale(Vue, locales); | ||
Override(Vue, langVM); | ||
Config(Vue, langVM); | ||
Extend(Vue); | ||
} | ||
/** | ||
* defineConfig | ||
* | ||
* This function define `lang` property to `Vue.config`. | ||
* | ||
* @param {Object} config | ||
* @param {String} lang | ||
* @private | ||
*/ | ||
function setupLangVM(Vue, lang) { | ||
var silent = Vue.config.silent; | ||
Vue.config.silent = true; | ||
if (!langVM) { | ||
langVM = new Vue({ data: { lang: lang } }); | ||
} | ||
Vue.config.silent = silent; | ||
} | ||
function defineConfig(config, lang) { | ||
Object.defineProperty(config, 'lang', { | ||
get: function get() { | ||
return lang; | ||
}, | ||
set: function set(val) { | ||
lang = val; | ||
} | ||
}); | ||
function setupLocale(Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, function (locale, lang) { | ||
Vue.locale(lang, locale); | ||
}); | ||
} | ||
} | ||
plugin.version = '2.4.1'; | ||
plugin.version = '3.0.0'; | ||
@@ -244,0 +560,0 @@ return plugin; |
/*! | ||
* vue-i18n v2.4.1 | ||
* vue-i18n v3.0.0 | ||
* (c) 2016 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/ | ||
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):r.VueI18n=n()}(this,function(){"use strict";function r(r){for(var n=arguments.length,t=Array(n>1?n-1:0),e=1;n>e;e++)t[e-1]=arguments[e];return 1===t.length&&"object"===f["typeof"](t[0])&&(t=t[0]),t&&t.hasOwnProperty||(t={}),r.replace(u,function(n,e,o,i){var f=void 0;return"{"===r[i-1]&&"}"===r[i+n.length]?o:(f=t.hasOwnProperty(o)?t[o]:null,null===f||void 0===f?"":f)})}function n(r){var n=r.split("."),t=n.splice(0,2);return t.push(n.join(".")),t}function t(r,t){for(var e=n(r),o=n(t),i=0;3>i;i++){var f=parseInt(e[i]||0,10),u=parseInt(o[i]||0,10);if(f>u)return 1;if(u>f)return-1}if((e[2]+o[2]+"").indexOf("-")>-1){var c=(a.exec(e[2])||[""])[0],l=(a.exec(o[2])||[""])[0];if(""===c)return 1;if(""===l)return-1;if(c>l)return 1;if(l>c)return-1}return 0}function e(n,e){function o(n,t,o){var f=n;try{var u=i(e[t],n)||e[t][n];f=(o?r(u,o):u)||n}catch(a){f=n}return f}var i=n.version&&-1===t("1.0.8",n.version)?n.parsers.path.getPath:n.parsers.path.get,f=n.util;return n.t=function(r){for(var t=arguments.length,e=Array(t>1?t-1:0),i=1;t>i;i++)e[i-1]=arguments[i];if(!r)return"";var u=n.config.lang;return 1===e.length?f.isObject(e[0])||f.isArray(e[0])?e=e[0]:"string"==typeof e[0]&&(u=e[0]):2===e.length&&("string"==typeof e[0]&&(u=e[0]),(f.isObject(e[1])||f.isArray(e[1]))&&(e=e[1])),o(r,u,e)},n.prototype.$t=function(r){for(var t=arguments.length,e=Array(t>1?t-1:0),o=1;t>o;o++)e[o-1]=arguments[o];return n.t.apply(n,[r].concat(e))},n}function o(r){var n=arguments.length<=1||void 0===arguments[1]?{lang:"en",locales:{}}:arguments[1];i(r.config,n.lang),e(r,n.locales)}function i(r,n){Object.defineProperty(r,"lang",{get:function(){return n},set:function(r){n=r}})}var f={};f["typeof"]=function(r){return r&&"undefined"!=typeof Symbol&&r.constructor===Symbol?"symbol":typeof r};var u=/(%|)\{([0-9a-zA-Z]+)\}/g,a=/-([\w-.]+)/;return o.version="2.4.1",o}); | ||
!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}); |
{ | ||
"name": "vue-i18n", | ||
"description": "Internationalization plugin for Vue.js", | ||
"version": "2.4.1", | ||
"version": "3.0.0", | ||
"author": { | ||
@@ -18,41 +18,41 @@ "name": "kazuya kawaguchi", | ||
"devDependencies": { | ||
"babel-core": "^6.2.1", | ||
"babel-loader": "^6.2.0", | ||
"babel-plugin-espower": "^2.0.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.13", | ||
"babel-preset-es2015": "^6.1.18", | ||
"babel-preset-es2015-rollup": "^1.0.0", | ||
"cz-conventional-changelog": "^1.1.4", | ||
"eslint": "^2.2.0", | ||
"babel-core": "^6.7.4", | ||
"babel-loader": "^6.2.4", | ||
"babel-plugin-espower": "^2.1.2", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.7.4", | ||
"babel-polyfill": "^6.7.4", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"eslint": "^2.5.1", | ||
"eslint-config-standard": "^5.1.0", | ||
"eslint-loader": "^1.3.0", | ||
"eslint-plugin-promise": "^1.0.8", | ||
"eslint-plugin-promise": "^1.1.0", | ||
"eslint-plugin-standard": "^1.3.2", | ||
"espower-babel": "^4.0.0", | ||
"istanbul-instrumenter-loader": "^0.1.3", | ||
"istanbul-instrumenter-loader": "^0.2.0", | ||
"json-loader": "^0.5.4", | ||
"karma": "^0.13.9", | ||
"karma-chrome-launcher": "^0.2.0", | ||
"karma-coverage": "^0.2.6", | ||
"karma": "^0.13.22", | ||
"karma-chrome-launcher": "^0.2.3", | ||
"karma-coverage": "^0.5.5", | ||
"karma-coveralls": "^1.1.2", | ||
"karma-firefox-launcher": "^0.1.6", | ||
"karma-mocha": "^0.2.0", | ||
"karma-mocha-reporter": "^1.1.1", | ||
"karma-mocha": "^0.2.2", | ||
"karma-mocha-reporter": "^2.0.0", | ||
"karma-phantomjs-launcher": "^0.2.1", | ||
"karma-safari-launcher": "^0.1.1", | ||
"karma-sauce-launcher": "^0.2.14", | ||
"karma-sourcemap-loader": "^0.3.5", | ||
"karma-sauce-launcher": "^0.3.1", | ||
"karma-sourcemap-loader": "^0.3.7", | ||
"karma-webpack": "^1.7.0", | ||
"mocha": "^2.3.4", | ||
"mocha": "^2.4.5", | ||
"mocha-generators": "^1.2.0", | ||
"mocha-loader": "^0.7.1", | ||
"nightmare": "^2.0.8", | ||
"phantomjs": "^1.9.19", | ||
"power-assert": "^1.2.0", | ||
"rollup": "^0.21.1", | ||
"rollup-plugin-babel": "^2.2.0", | ||
"nightmare": "^2.2.0", | ||
"phantomjs": "^2.1.3", | ||
"power-assert": "^1.3.1", | ||
"rollup": "^0.25.6", | ||
"rollup-plugin-babel": "^2.4.0", | ||
"rollup-plugin-replace": "^1.1.0", | ||
"uglify-js": "^2.6.1", | ||
"vue": "^0.12.0", | ||
"webpack": "^1.12.9", | ||
"webpack-dev-server": "^1.14.0" | ||
"vue": "^1.0.0", | ||
"webpack": "^1.12.14", | ||
"webpack-dev-server": "^1.14.1" | ||
}, | ||
@@ -88,4 +88,5 @@ "files": [ | ||
"coveralls": "VUE_I18N_TYPE=coveralls karma start config/karma.conf.js", | ||
"proto": "webpack-dev-server --quite --config config/webpack.dev.conf.js --host 0.0.0.0", | ||
"dev": "webpack-dev-server --quiet --config config/webpack.test.conf.js", | ||
"e2e": "webpack-dev-server --quiet --config config/webpack.e2e.conf.js & mocha -t 20000 --compilers js:espower-babel/guess test/e2e/test.js && kill $! || (kill $! && exit 1)", | ||
"e2e": "webpack-dev-server --quiet --config config/webpack.e2e.conf.js & mocha --opts test/e2e/mocha.opts --harmony test/e2e/test.js && kill $! || (kill $! && exit 1)", | ||
"ie": "VUE_I18N_TYPE=sauce SAUCE=batch2 karma start config/karma.conf.js", | ||
@@ -92,0 +93,0 @@ "lint": "eslint src/*.js test/**/*.js config/*.js", |
255
README.md
@@ -7,3 +7,2 @@ # vue-i18n | ||
[![Sauce Test Status](https://saucelabs.com/buildstatus/vue-i18n)](https://saucelabs.com/u/vue-i18n) | ||
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) | ||
@@ -14,4 +13,4 @@ | ||
# Requirements | ||
- works with Vue.js `0.12.0`+ | ||
# Compatibility | ||
- Vue.js `1.0.0`+ | ||
@@ -33,3 +32,5 @@ | ||
Vue.use(VueI18n, { ... }) | ||
Vue.use(VueI18n) | ||
Vue.config.lang = 'ja' | ||
Vue.locale('ja', { ... }) | ||
``` | ||
@@ -41,3 +42,3 @@ | ||
var Vue = require('vue') | ||
var i18n = require('vue-i18n') | ||
var VueI18n = require('vue-i18n') | ||
@@ -48,3 +49,3 @@ // ready translated locales | ||
message: { | ||
hello: 'the world' | ||
hello: 'hello world' | ||
} | ||
@@ -54,3 +55,3 @@ }, | ||
message: { | ||
hello: 'ザ・ワールド' | ||
hello: 'こんにちは、世界' | ||
} | ||
@@ -60,12 +61,24 @@ } | ||
// set plugin | ||
Vue.use(i18n, { | ||
// install plugin | ||
// DEPRECATED: | ||
// `options` arguments, please use `Vue.config.lang` and `Vue.locale`. | ||
// 3.1 or later, not used `options` arguments!! | ||
Vue.use(VueI18n/*, { | ||
lang: 'ja', | ||
locales: locales | ||
}*/) | ||
// set lang | ||
// RECOMMEND: 3.0 or later | ||
Vue.config.lang = 'ja' | ||
// set locales | ||
// RECOMMEND: 3.0 or later | ||
Object.keys(locales).forEach(function (lang) { | ||
Vue.locale(lang, locales[lang]) | ||
}) | ||
// create instance | ||
new Vue({ | ||
el: '#test-i18n' | ||
}) | ||
new Vue({ el: 'body' }) | ||
``` | ||
@@ -76,5 +89,3 @@ | ||
```html | ||
<div id="test-i18n" class="message"> | ||
<p>{{ $t("message.hello") }}</p> | ||
</div> | ||
<p>{{ $t("message.hello") }}</p> | ||
``` | ||
@@ -85,5 +96,3 @@ | ||
```html | ||
<div id="test-i18n" class="message"> | ||
<p>ザ・ワールド</p> | ||
</div> | ||
<p>こんにちは、世界</p> | ||
``` | ||
@@ -93,2 +102,3 @@ | ||
# Formatting | ||
## HTML formatting | ||
@@ -110,5 +120,3 @@ In some cases you might want to rendered your translation as an HTML message and not a static string. | ||
```html | ||
<div class="message"> | ||
<p>{{{ $t('message.hello') }}}</p> | ||
</div> | ||
<p>{{{ $t('message.hello') }}}</p> | ||
``` | ||
@@ -119,7 +127,5 @@ | ||
```html | ||
<div class="message"> | ||
<p>hello | ||
<!--<br> exists but is rendered as html and not a string--> | ||
world</p> | ||
</div> | ||
<p>hello | ||
<!--<br> exists but is rendered as html and not a string--> | ||
world</p> | ||
``` | ||
@@ -144,5 +150,3 @@ | ||
```html | ||
<div class="message"> | ||
<p>{{ $t('message.hello', { msg: "hello"}) }}</p> | ||
</div> | ||
<p>{{ $t('message.hello', { msg: "hello"}) }}</p> | ||
``` | ||
@@ -153,5 +157,3 @@ | ||
```html | ||
<div class="message"> | ||
<p>hello world</p> | ||
</div> | ||
<p>hello world</p> | ||
``` | ||
@@ -176,5 +178,3 @@ | ||
```html | ||
<div class="message"> | ||
<p>{{ $t('message.hello', ["hello"]) }}</p> | ||
</div> | ||
<p>{{ $t('message.hello', ["hello"]) }}</p> | ||
``` | ||
@@ -185,9 +185,5 @@ | ||
```html | ||
<div class="message"> | ||
<p>hello world</p> | ||
</div> | ||
<p>hello world</p> | ||
``` | ||
# Interpolation format | ||
## Support ruby on rails i18n format | ||
@@ -210,5 +206,3 @@ | ||
```html | ||
<div class="message"> | ||
<p>{{ $t('message.hello', { msg: "hello"}) }}</p> | ||
</div> | ||
<p>{{ $t('message.hello', { msg: "hello"}) }}</p> | ||
``` | ||
@@ -219,23 +213,133 @@ | ||
```html | ||
<div class="message"> | ||
<p>hello world</p> | ||
</div> | ||
<p>hello world</p> | ||
``` | ||
# Dynamic locale | ||
# API | ||
Sometimes, you need to set dynamically the locale from external location. You can set dynamically it with `Vue.locale`. | ||
the below the example: | ||
```javascript | ||
var self = this | ||
var lang = 'ja' | ||
Vue.locale(lang, function () { | ||
self.loading = true | ||
return fetch('/locale/' + lang, { | ||
method: 'get', | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}).then(function (res) { | ||
return res.json() | ||
}).then(function (json) { | ||
self.loading = false | ||
if (Object.keys(json).length === 0) { | ||
return Promise.reject(new Error('locale empty !!')) | ||
} else { | ||
return Promise.resolve(json) | ||
} | ||
}).catch(function (error) { | ||
self.error = error.message | ||
return Promise.reject() | ||
}) | ||
}, function () { | ||
Vue.config.lang = lang | ||
}) | ||
``` | ||
## Dynamic locale interfaces | ||
In dynamic locales, You can use the two type interfaces: | ||
### 1. function | ||
You need to implement locale setting that return function have `function (resolve, reject)` like promise (future). The following, those argument of the function, if successful, you need to use the `resolve` according to locale object. if failed, you need to use `reject` | ||
- successful: `resolve` | ||
- failed: `reject` | ||
### 2. promise | ||
As mentioned above, You need to implement locale setting that return a promise. if successful, you need to `resolve` according to locale object. if failed, you need to use `reject`. | ||
# API References | ||
## Global Config | ||
### lang | ||
- **Type:** `String` | ||
- **Default:** `en` | ||
- **Usage:** | ||
Get or set a translation language code. Default by `en` string value. | ||
```javascript | ||
Vue.config.lang = 'ja' | ||
``` | ||
## Global Methods | ||
### Vue.locale ( lang, [locale], [cb] ) | ||
- **Arguments:** | ||
- `{String} lang` | ||
- `{Object | Function} [locale]` | ||
- `{Function} [cb]` | ||
- **Return:** | ||
- locale function or object | ||
- **Usage:** | ||
Register or retrieve a locale | ||
```javascript | ||
// register locale with object | ||
Vue.locale('en', { message: 'hello' }) | ||
// register with external locale | ||
Vue.locale('ja', function () { | ||
return fetch('/locales/ja', { | ||
method: 'get', | ||
// ... | ||
}).then(function (json) { | ||
return Promise.resolve(json) | ||
}).catch(function (error) { | ||
return Promise.reject() | ||
}) | ||
}, function () { | ||
Vue.config.lang = 'ja' | ||
}) | ||
``` | ||
### Vue.t( keypath, [lang], [arguments] ) | ||
- **Arguments:** | ||
- `{String} keypath` | ||
- `{String} [lang]` | ||
- `{Array | Object [arguments]` | ||
- **Return:** | ||
Translated string | ||
- **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) | ||
## Instance Methods | ||
## $t(keypath, [lang], [arguments]) | ||
- keypath: `String` **required** | ||
- lang: `String` **optional** | ||
- arguments: `Array | Object` **optional** | ||
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). | ||
- **Arguments:** | ||
- `{String} keypath` | ||
- `{String} [lang]` | ||
- `{Array | Object [arguments]` | ||
## Vue.t(keypath, [lang], [arguments]) | ||
- keypath: `String` **required** | ||
- lang: `String` **optional** | ||
- arguments: `Array | Object` **optional** | ||
- **Return:** | ||
Translated string | ||
This is the same as the `$t` method. This is translate function for global. | ||
- **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). | ||
@@ -245,2 +349,4 @@ | ||
> NOTE: Deprecated in 3.1 or later :warning: | ||
## Plugin options | ||
@@ -274,10 +380,2 @@ | ||
# Configuration | ||
## Vue.config.lang | ||
Get or set a global translation language code. Default by `en` string value. You can change the language of the global level dynamic translation in your application. | ||
When specified with `lang` plugins option at `Vue.use`, `Vue.config.lang` is set that value. | ||
# Contributing | ||
@@ -291,13 +389,30 @@ - Fork it ! | ||
# Testing | ||
# Development Setup | ||
```shell | ||
$ npm run unit | ||
``` | ||
# install deps | ||
npm install | ||
# build dist files | ||
npm run build | ||
# lint | ||
npm run lint | ||
# run unit tests only | ||
npm run unit | ||
# run e2e tests only | ||
npm run e2e | ||
# lint & run all tests | ||
npm test | ||
# Changelog | ||
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/kazupon/vue-i18n/blob/dev/CHANGELOG.md). | ||
# License | ||
## MIT | ||
[MIT](http://opensource.org/licenses/MIT) |
@@ -9,8 +9,7 @@ import format from './format' | ||
* @param {Vue} Vue | ||
* @param {Object} locales | ||
* @return {Vue} | ||
*/ | ||
export default function (Vue, locales) { | ||
const getPath = (Vue.version && compare('1.0.8', Vue.version) === -1) | ||
export default function (Vue) { | ||
const getPath = (compare('1.0.8', Vue.version) === -1) | ||
? Vue.parsers.path.getPath | ||
@@ -23,3 +22,4 @@ : Vue.parsers.path.get | ||
try { | ||
let val = getPath(locales[lang], key) || locales[lang][key] | ||
let locale = Vue.locale(lang) | ||
let val = getPath(locale, key) || locale[key] | ||
value = (args ? format(val, args) : val) || key | ||
@@ -26,0 +26,0 @@ } catch (e) { |
@@ -30,4 +30,4 @@ /** | ||
if (string[index - 1] === '{' && | ||
string[index + match.length] === '}') { | ||
if (string[index - 1] === '{' | ||
&& string[index + match.length] === '}') { | ||
return i | ||
@@ -34,0 +34,0 @@ } else { |
@@ -1,4 +0,11 @@ | ||
import extend from './extend' | ||
import util, { warn, empty, each } from './util' | ||
import compare from './compare' | ||
import Asset from './asset' | ||
import Override from './override' | ||
import Config from './config' | ||
import Extend from './extend' | ||
let langVM // singleton | ||
/** | ||
@@ -11,27 +18,56 @@ * plugin | ||
function plugin (Vue, opts = { lang: 'en', locales: {} }) { | ||
defineConfig(Vue.config, opts.lang) | ||
extend(Vue, opts.locales) | ||
} | ||
function plugin (Vue, opts = {}) { | ||
if (process.env.NODE_ENV !== 'production' && plugin.installed) { | ||
warn('already installed.') | ||
return | ||
} | ||
if (process.env.NODE_ENV !== 'production' | ||
&& (!Vue.version || compare(Vue.version, '1.0') < 0)) { | ||
warn('vue-i18n (' + plugin.version | ||
+ ') need to use vue version 1.0 or later (vue version: ' | ||
+ Vue.version + ').') | ||
return | ||
} | ||
/** | ||
* defineConfig | ||
* | ||
* This function define `lang` property to `Vue.config`. | ||
* | ||
* @param {Object} config | ||
* @param {String} lang | ||
* @private | ||
*/ | ||
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' | ||
function defineConfig (config, lang) { | ||
Object.defineProperty(config, 'lang', { | ||
get: () => { return lang }, | ||
set: (val) => { lang = val } | ||
}) | ||
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 | ||
setupLangVM(Vue, lang) | ||
Asset(Vue) | ||
setupLocale(Vue, locales) | ||
Override(Vue, langVM) | ||
Config(Vue, langVM) | ||
Extend(Vue) | ||
} | ||
plugin.version = '2.4.1' | ||
function setupLangVM (Vue, lang) { | ||
const silent = Vue.config.silent | ||
Vue.config.silent = true | ||
if (!langVM) { | ||
langVM = new Vue({ data: { lang: lang } }) | ||
} | ||
Vue.config.silent = silent | ||
} | ||
function setupLocale (Vue, locales) { | ||
if (!empty(locales)) { | ||
each(locales, (locale, lang) => { | ||
Vue.locale(lang, locale) | ||
}) | ||
} | ||
} | ||
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
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
53886
16
1375
398
4
1