From 21e595783c90915b2b4fcb52b26194e66eaf2974 Mon Sep 17 00:00:00 2001 From: mmochetti Date: Sat, 27 Aug 2016 13:10:37 -0300 Subject: [PATCH] Adding linked translations --- src/extend.js | 19 ++++++++++++++++++- test/specs/fixture/locales.js | 6 +++++- test/specs/i18n.js | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/extend.js b/src/extend.js index 9fe4ae2f9..390e3faaa 100644 --- a/src/extend.js +++ b/src/extend.js @@ -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 } diff --git a/test/specs/fixture/locales.js b/test/specs/fixture/locales.js index 100cfe2f7..dca99e134 100644 --- a/test/specs/fixture/locales.js +++ b/test/specs/fixture/locales.js @@ -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}', diff --git a/test/specs/i18n.js b/test/specs/i18n.js index e95cba61a..c9b5efade 100644 --- a/test/specs/i18n.js +++ b/test/specs/i18n.js @@ -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)