Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit f7ae073

Browse files
mochettskazupon
authored andcommitted
⭐ new: add linked translations (#50) by @mmochetti
1 parent 3f53617 commit f7ae073

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/extend.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,26 @@ export default function (Vue) {
4040
function interpolate (locale, key, args) {
4141
if (!locale) { return null }
4242

43-
const val = getValue(locale, key) || locale[key]
43+
let val = getValue(locale, key) || locale[key]
4444
if (!val) { return null }
4545

46+
// Check for the existance of links within the translated string
47+
if (val.indexOf('@:') >= 0) {
48+
// Match all the links within the local
49+
// We are going to replace each of
50+
// them with its translation
51+
const matches = val.match(/(@:[\w|\.]+)/g)
52+
for (const idx in matches) {
53+
const link = matches[idx]
54+
// Remove the leading @:
55+
const linkPlaceholder = link.substr(2)
56+
// Translate the link
57+
const translatedstring = interpolate(locale, linkPlaceholder, args)
58+
// Replace the link with the translated string
59+
val = val.replace(link, translatedstring)
60+
}
61+
}
62+
4663
return args ? format(val, args) : val
4764
}
4865

test/specs/fixture/locales.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export default {
77
named: 'Hello {name}, how are you?',
88
list: 'Hello {0}, how are you?'
99
},
10-
fallback: 'this is fallback'
10+
fallback: 'this is fallback',
11+
link: '@:message.hello',
12+
link_end: 'This is a linked translation to @:message.hello',
13+
link_within: 'Isn\'t @:message.hello we live in great?',
14+
link_multiple: 'Hello @:message.hoge!, isn\'t @:message.hello great?'
1115
},
1216
'hello world': 'Hello World',
1317
'Hello {0}': 'Hello {0}',

test/specs/i18n.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ describe('i18n', () => {
2121
})
2222
})
2323

24+
describe('linked translation', () => {
25+
it('should translate simple link', () => {
26+
assert.equal(Vue.t('message.link'), locales.en.message.hello)
27+
})
28+
})
29+
30+
describe('linked translation', () => {
31+
it('should translate link at the end of locale', () => {
32+
assert.equal(Vue.t('message.link_end'), 'This is a linked translation to the world')
33+
})
34+
})
35+
36+
describe('linked translation', () => {
37+
it('should translate link within a locale', () => {
38+
assert.equal(Vue.t('message.link_within'), 'Isn\'t the world we live in great?')
39+
})
40+
})
41+
42+
describe('linked translation', () => {
43+
it('should translate multiple links within a locale', () => {
44+
assert.equal(Vue.t('message.link_multiple'), 'Hello hoge!, isn\'t the world great?')
45+
})
46+
})
47+
2448
describe('ja language locale', () => {
2549
it('should translate a japanese', () => {
2650
assert.equal(Vue.t('message.hello', 'ja'), locales.ja.message.hello)

0 commit comments

Comments
 (0)