Comparing version 1.0.0 to 1.1.0
@@ -5,3 +5,3 @@ { | ||
"description": "Internationalization plugin of Vue.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/kazupon/vue-i18n", | ||
@@ -8,0 +8,0 @@ "authors": [ |
{ | ||
"name": "vue-i18n", | ||
"repo": "kazupon/vue-i18n", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Internationalization plugin of Vue.js", | ||
@@ -16,2 +16,3 @@ "license": "MIT", | ||
"scripts": [ | ||
"lib/format.js", | ||
"index.js" | ||
@@ -18,0 +19,0 @@ ], |
/** | ||
* vue-i18n v1.0.0 | ||
* vue-i18n v1.1.0 | ||
* (c) 2015 kazuya kawaguchi | ||
@@ -64,3 +64,17 @@ * Released under the MIT License. | ||
/** | ||
* Expose internationalization plugin | ||
* Import(s) | ||
*/ | ||
var format = __webpack_require__(1) | ||
/** | ||
* Export(s) | ||
*/ | ||
module.exports = plugin | ||
/** | ||
* plugin | ||
* | ||
@@ -71,3 +85,3 @@ * @param {Object} Vue | ||
module.exports = function (Vue, opts) { | ||
function plugin (Vue, opts) { | ||
opts = opts || {} | ||
@@ -77,7 +91,29 @@ var lang = opts.lang || 'en' | ||
// for Vue 0.11.4 later | ||
// `$t` method (for Vue 0.11.4 later) | ||
try { | ||
var path = Vue.parsers.path | ||
var util = Vue.util | ||
Vue.prototype.$t = function (key) { | ||
return key ? (path.get(locales[lang], key) || key) : '' | ||
if (!key) { return '' } | ||
var args = null | ||
var language = lang | ||
if (arguments.length === 2) { | ||
if (util.isObject(arguments[1]) || util.isArray(arguments[1])) { | ||
args = arguments[1] | ||
} else if (typeof arguments[1] === 'string') { | ||
language = arguments[1] | ||
} | ||
} else if (arguments.length === 3) { | ||
if (typeof arguments[1] === 'string') { | ||
language = arguments[1] | ||
} | ||
if (util.isObject(arguments[2]) || util.isArray(arguments[2])) { | ||
args = arguments[2] | ||
} | ||
} | ||
var val = path.get(locales[language], key) | ||
return (args ? format(val, args) : val) || key | ||
} | ||
@@ -88,2 +124,3 @@ } catch (e) { | ||
// 't' function | ||
Vue.t = function (key) { | ||
@@ -107,2 +144,3 @@ var ret = key || '' | ||
// 'v-t' directive | ||
Vue.directive('t', { | ||
@@ -119,4 +157,73 @@ isLiteral: true, | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/** | ||
* String format template | ||
* - Inspired: | ||
* https://github.com/Matt-Esch/string-template/index.js | ||
*/ | ||
/** | ||
* Import(s) | ||
*/ | ||
var slice = Array.prototype.slice | ||
/** | ||
* Constant(s) | ||
*/ | ||
var RE_NARGS = /\{([0-9a-zA-Z]+)\}/g | ||
/** | ||
* Export(s) | ||
*/ | ||
module.exports = template | ||
/** | ||
* template | ||
* | ||
* @param {String} string | ||
* @return {String} | ||
*/ | ||
function template (string) { | ||
var args | ||
if (arguments.length === 2 && typeof arguments[1] === 'object') { | ||
args = arguments[1] | ||
} else { | ||
args = slice.call(arguments, 1) | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {} | ||
} | ||
return string.replace(RE_NARGS, function (match, i, index) { | ||
var result | ||
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 | ||
} | ||
}) | ||
} | ||
/***/ } | ||
/******/ ]) | ||
}); |
/** | ||
* vue-i18n v1.0.0 | ||
* vue-i18n v1.1.0 | ||
* (c) 2015 kazuya kawaguchi | ||
@@ -7,2 +7,2 @@ * Released under the MIT License. | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):"object"==typeof exports?exports["vue-i18n"]=e():t["vue-i18n"]=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t){t.exports=function(t,e){e=e||{};var n=e.lang||"en",o=e.locales||e.resources||{};try{var r=t.parsers.path;t.prototype.$t=function(t){return t?r.get(o[n],t)||t:""}}catch(i){t.utils.warn("not support $t in this Vue version")}t.t=function(t){var e=t||"",r=o[n];if(t&&r)for(var i=t.split("."),s=0;s<i.length;s++){if(r=r[i[s]],!r){e=t;break}e=r}return e},t.directive("t",{isLiteral:!0,bind:function(){1===this.el.nodeType&&(this.el.textContent=t.t(this.expression))}})}}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):"object"==typeof exports?exports["vue-i18n"]=e():t["vue-i18n"]=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){function n(t,e){e=e||{};var r=e.lang||"en",n=e.locales||e.resources||{};try{var s=t.parsers.path,u=t.util;t.prototype.$t=function(t){if(!t)return"";var e=null,a=r;2===arguments.length?u.isObject(arguments[1])||u.isArray(arguments[1])?e=arguments[1]:"string"==typeof arguments[1]&&(a=arguments[1]):3===arguments.length&&("string"==typeof arguments[1]&&(a=arguments[1]),(u.isObject(arguments[2])||u.isArray(arguments[2]))&&(e=arguments[2]));var i=s.get(n[a],t);return(e?o(i,e):i)||t}}catch(a){t.utils.warn("not support $t in this Vue version")}t.t=function(t){var e=t||"",o=n[r];if(t&&o)for(var s=t.split("."),u=0;u<s.length;u++){if(o=o[s[u]],!o){e=t;break}e=o}return e},t.directive("t",{isLiteral:!0,bind:function(){1===this.el.nodeType&&(this.el.textContent=t.t(this.expression))}})}var o=r(1);t.exports=n},function(t){function e(t){var e;return e=2===arguments.length&&"object"==typeof arguments[1]?arguments[1]:r.call(arguments,1),e&&e.hasOwnProperty||(e={}),t.replace(n,function(r,n,o){var s;return"{"===t[o-1]&&"}"===t[o+r.length]?n:(s=e.hasOwnProperty(n)?e[n]:null,null===s||void 0===s?"":s)})}var r=Array.prototype.slice,n=/\{([0-9a-zA-Z]+)\}/g;t.exports=e}])}); |
@@ -0,1 +1,6 @@ | ||
# v1.1.0 / 2015-01-10 | ||
* Support template string in `$t` method | ||
* Support language changing in `$t` method | ||
# v1.0.0 / 2015-01-10 | ||
@@ -2,0 +7,0 @@ |
46
index.js
/** | ||
* Expose internationalization plugin | ||
* Import(s) | ||
*/ | ||
var format = require('./lib/format') | ||
/** | ||
* Export(s) | ||
*/ | ||
module.exports = plugin | ||
/** | ||
* plugin | ||
* | ||
@@ -8,3 +22,3 @@ * @param {Object} Vue | ||
module.exports = function (Vue, opts) { | ||
function plugin (Vue, opts) { | ||
opts = opts || {} | ||
@@ -14,7 +28,29 @@ var lang = opts.lang || 'en' | ||
// for Vue 0.11.4 later | ||
// `$t` method (for Vue 0.11.4 later) | ||
try { | ||
var path = Vue.parsers.path | ||
var util = Vue.util | ||
Vue.prototype.$t = function (key) { | ||
return key ? (path.get(locales[lang], key) || key) : '' | ||
if (!key) { return '' } | ||
var args = null | ||
var language = lang | ||
if (arguments.length === 2) { | ||
if (util.isObject(arguments[1]) || util.isArray(arguments[1])) { | ||
args = arguments[1] | ||
} else if (typeof arguments[1] === 'string') { | ||
language = arguments[1] | ||
} | ||
} else if (arguments.length === 3) { | ||
if (typeof arguments[1] === 'string') { | ||
language = arguments[1] | ||
} | ||
if (util.isObject(arguments[2]) || util.isArray(arguments[2])) { | ||
args = arguments[2] | ||
} | ||
} | ||
var val = path.get(locales[language], key) | ||
return (args ? format(val, args) : val) || key | ||
} | ||
@@ -25,2 +61,3 @@ } catch (e) { | ||
// 't' function | ||
Vue.t = function (key) { | ||
@@ -44,2 +81,3 @@ var ret = key || '' | ||
// 'v-t' directive | ||
Vue.directive('t', { | ||
@@ -46,0 +84,0 @@ isLiteral: true, |
@@ -16,2 +16,3 @@ // Karma configuration | ||
'node_modules/vue/dist/vue.js', | ||
'lib/*.js', | ||
'index.js', | ||
@@ -28,2 +29,3 @@ 'test/**/specs/*.js' | ||
'node_modules/vue/dist/vue.js': ['commonjs'], | ||
'lib/*.js': ['coverage', 'commonjs'], | ||
'index.js': ['coverage', 'commonjs'], | ||
@@ -30,0 +32,0 @@ 'test/**/specs/*.js': ['commonjs'] |
{ | ||
"name": "vue-i18n", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Internationalization plugin of Vue.js", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"mocha": "^1.18.2", | ||
"vue": "~0.11.2", | ||
"vue": "~0.11.4", | ||
"webpack": "^1.4.13", | ||
@@ -32,0 +32,0 @@ "karma": "^0.12.24", |
@@ -34,3 +34,3 @@ /** | ||
}, function (html) { | ||
expect(html).to.be.eql('bar<br>buz') | ||
expect(html).to.be.eql('Hello kazupon !!<br>How are you?') | ||
}) | ||
@@ -37,0 +37,0 @@ .run(done) |
@@ -14,3 +14,7 @@ /** | ||
hello: 'the world', | ||
hoge: 'hoge' | ||
hoge: 'hoge', | ||
format: { | ||
named: 'Hello {name}, how are you?', | ||
list: 'Hello {0}, how are you?' | ||
} | ||
} | ||
@@ -21,3 +25,7 @@ }, | ||
hello: 'ザ・ワールド', | ||
hoge: 'ほげ' | ||
hoge: 'ほげ', | ||
format: { | ||
named: 'こんにちは {name}, ごきげんいかが?', | ||
list: 'こんにちは {0}, ごきげんいかが?' | ||
} | ||
} | ||
@@ -29,3 +37,3 @@ } | ||
describe('$t', function () { | ||
describe('en', function () { | ||
describe('en language locale', function () { | ||
it('should translate an english', function () { | ||
@@ -42,3 +50,3 @@ Vue.use(i18n, { | ||
describe('ja', function () { | ||
describe('ja language locale', function () { | ||
it('should translate a japanese', function () { | ||
@@ -55,3 +63,3 @@ Vue.use(i18n, { | ||
describe('key', function () { | ||
describe('key argument', function () { | ||
before(function () { | ||
@@ -78,3 +86,3 @@ Vue.use(i18n, { | ||
describe('not found', function () { | ||
describe('not regist key', function () { | ||
it('should return key string', function () { | ||
@@ -86,2 +94,52 @@ var vm = new Vue() | ||
}) | ||
describe('format arguments', function () { | ||
before(function () { | ||
Vue.use(i18n, { | ||
lang: 'en', | ||
locales: locales | ||
}) | ||
}) | ||
describe('named', function () { | ||
it('should return replaced string', function () { | ||
var vm = new Vue() | ||
expect(vm.$t('message.format.named', { name: 'kazupon' })) | ||
.to.be.eql('Hello kazupon, how are you?') | ||
}) | ||
}) | ||
describe('list', function () { | ||
it('should return replaced string', function () { | ||
var vm = new Vue() | ||
expect(vm.$t('message.format.list', ['kazupon'])) | ||
.to.be.eql('Hello kazupon, how are you?') | ||
}) | ||
}) | ||
}) | ||
describe('language argument', function () { | ||
it('should return empty string', function () { | ||
Vue.use(i18n, { | ||
lang: 'en', | ||
locales: locales | ||
}) | ||
var vm = new Vue() | ||
expect(vm.$t('message.hello', 'ja')).to.be.eql(locales.ja.message.hello) | ||
}) | ||
}) | ||
describe('format & language arguments', function () { | ||
it('should return replaced string', function () { | ||
Vue.use(i18n, { | ||
lang: 'en', | ||
locales: locales | ||
}) | ||
var vm = new Vue() | ||
expect(vm.$t('message.format.list', 'ja', ['kazupon'])) | ||
.to.be.eql('こんにちは kazupon, ごきげんいかが?') | ||
}) | ||
}) | ||
}) | ||
@@ -88,0 +146,0 @@ |
@@ -1,2 +0,2 @@ | ||
- [ ] Template Strings format | ||
- [ ] Tynamic language changing | ||
- [x] Template Strings format | ||
- [x] language changing |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
1057
0
39756
44