Skip to content

Commit

Permalink
⭐ new: component locales
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
kazupon committed May 8, 2016
1 parent d87b59b commit 12fe695
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,33 @@ import { getValue } from './path'
export default function (Vue) {
const { isArray, isObject } = Vue.util

function getVal (key, lang, args) {
let value = key
try {
let locale = Vue.locale(lang)
let val = getValue(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
}


Expand All @@ -36,23 +53,8 @@ export default function (Vue) {
Vue.t = (key, ...args) => {
if (!key) { return '' }

let language = Vue.config.lang
if (args.length === 1) {
if (isObject(args[0]) || 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 (isObject(args[1]) || isArray(args[1])) {
args = args[1]
}
}

return getVal(key, language, args)
const { lang, params } = parseArgs(...args)
return translate(Vue.locale(lang), key, params) || key
}


Expand All @@ -64,8 +66,13 @@ export default function (Vue) {
* @return {String}
*/

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)
|| key
}

return Vue
Expand Down
50 changes: 50 additions & 0 deletions test/specs/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from 'power-assert'
import Vue from 'vue'
import locales from './fixture/locales'


describe('component locales', () => {
before((done) => {
Object.keys(locales).forEach((lang) => {
Vue.locale(lang, locales[lang])
})
Vue.config.lang = 'en'
Vue.nextTick(done)
})

let vm
beforeEach((done) => {
vm = new Vue({
el: document.createElement('div'),
template: '<div><component1></component1></div>',
components: {
component1: {
locales: {
en: {
foo: {
bar: {
buz: 'hello world'
}
}
}
}
}
}
})
vm.$nextTick(done)
})

describe('local', () => {
it('should be translated', () => {
const comp1 = vm.$children[0] // component1
assert(comp1.$t('foo.bar.buz') === 'hello world')
})
})

describe('global', () => {
it('should be translated', () => {
const comp1 = vm.$children[0] // component1
assert(comp1.$t('message.hello') === 'the world')
})
})
})
1 change: 1 addition & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Vue.use(plugin)

require('./i18n')
require('./asset')
require('./component')

0 comments on commit 12fe695

Please sign in to comment.