Skip to content

Commit

Permalink
⭐ new: add linked translations (#50) by @mmochetti
Browse files Browse the repository at this point in the history
  • Loading branch information
mochetts authored and kazupon committed Aug 28, 2016
1 parent 3f53617 commit f7ae073
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,26 @@ export default function (Vue) {
function interpolate (locale, key, args) {
if (!locale) { return null }

const val = getValue(locale, key) || locale[key]
let val = getValue(locale, key) || locale[key]
if (!val) { return null }

// Check for the existance of links within the translated string
if (val.indexOf('@:') >= 0) {
// Match all the links within the local
// We are going to replace each of
// them with its translation
const matches = val.match(/(@:[\w|\.]+)/g)
for (const idx in matches) {
const link = matches[idx]
// Remove the leading @:
const linkPlaceholder = link.substr(2)
// Translate the link
const translatedstring = interpolate(locale, linkPlaceholder, args)
// Replace the link with the translated string
val = val.replace(link, translatedstring)
}
}

return args ? format(val, args) : val
}

Expand Down
6 changes: 5 additions & 1 deletion test/specs/fixture/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default {
named: 'Hello {name}, how are you?',
list: 'Hello {0}, how are you?'
},
fallback: 'this is fallback'
fallback: 'this is fallback',
link: '@:message.hello',
link_end: 'This is a linked translation to @:message.hello',
link_within: 'Isn\'t @:message.hello we live in great?',
link_multiple: 'Hello @:message.hoge!, isn\'t @:message.hello great?'
},
'hello world': 'Hello World',
'Hello {0}': 'Hello {0}',
Expand Down
24 changes: 24 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ describe('i18n', () => {
})
})

describe('linked translation', () => {
it('should translate simple link', () => {
assert.equal(Vue.t('message.link'), locales.en.message.hello)
})
})

describe('linked translation', () => {
it('should translate link at the end of locale', () => {
assert.equal(Vue.t('message.link_end'), 'This is a linked translation to the world')
})
})

describe('linked translation', () => {
it('should translate link within a locale', () => {
assert.equal(Vue.t('message.link_within'), 'Isn\'t the world we live in great?')
})
})

describe('linked translation', () => {
it('should translate multiple links within a locale', () => {
assert.equal(Vue.t('message.link_multiple'), 'Hello hoge!, isn\'t the world great?')
})
})

describe('ja language locale', () => {
it('should translate a japanese', () => {
assert.equal(Vue.t('message.hello', 'ja'), locales.ja.message.hello)
Expand Down

0 comments on commit f7ae073

Please sign in to comment.