Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding linked translations #50

Merged
merged 1 commit into from
Aug 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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