Skip to content

Commit

Permalink
⚡ improvement(index): Allow escaping link key like @:(foo.bar). (#437)…
Browse files Browse the repository at this point in the history
… by @exoego

* Allow escaping the link to other key in the form of @:(key)

* Cache the regexp to avoid instantiation every time _link called.
  • Loading branch information
TATSUNO Yasuhiro authored and kazupon committed Oct 13, 2018
1 parent 6d45d86 commit acfc458
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const numberFormatKeys = [
'localeMatcher',
'formatMatcher'
]
const linkKeyMatcher = /(@:([\w\-_|.]+|\([\w\-_|.]+\)))/g
const bracketsMatcher = /[()]/g

export default class VueI18n {
static install: () => void
Expand Down Expand Up @@ -251,16 +253,16 @@ export default class VueI18n {
// Match all the links within the local
// We are going to replace each of
// them with its translation
const matches: any = ret.match(/(@:[\w\-_|.]+)/g)
const matches: any = ret.match(linkKeyMatcher)
for (const idx in matches) {
// ie compatible: filter custom array
// prototype method
if (!matches.hasOwnProperty(idx)) {
continue
}
const link: string = matches[idx]
// Remove the leading @:
const linkPlaceholder: string = link.substr(2)
// Remove the leading @: and the brackets
const linkPlaceholder: string = link.substr(2).replace(bracketsMatcher, '')
// Translate the link
let translated: any = this._interpolate(
locale, message, linkPlaceholder, host,
Expand Down
6 changes: 6 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('basic', () => {
})
})

describe('linked translation', () => {
it('should translate link with braces ', () => {
assert.strictEqual(i18n.t('message.linkBrackets'), 'Hello hoge. Isn\'t the world great?')
})
})

describe('ja locale', () => {
it('should translate a japanese', () => {
assert.equal(i18n.t('message.hello', 'ja'), messages.ja.message.hello)
Expand Down
1 change: 1 addition & 0 deletions test/unit/fixture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
linkEnd: 'This is a linked translation to @:message.hello',
linkWithin: 'Isn\'t @:message.hello we live in great?',
linkMultiple: 'Hello @:message.hoge!, isn\'t @:message.hello great?',
linkBrackets: 'Hello @:(message.hoge). Isn\'t @:(message.hello) great?',
linkHyphen: '@:hyphen-hello',
linkUnderscore: '@:underscore_hello',
linkList: '@:message.hello: {0} {1}',
Expand Down
32 changes: 32 additions & 0 deletions vuepress/guide/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,35 @@ Output the below:
```html
<p>DIO: the world !!!!</p>
```


### Grouping by brackets

A translation key of linked locale message can also have the form of `@:(message.foo.bar.baz)` in which the link to another translation key is within brackets `()`.

This can be useful if the link `@:message.something` is following by period `.`, which can be a part of link but in case it should not be.

Locale messages the below:

```js
const messages = {
en: {
message: {
dio: 'DIO',
linked: 'There\'s a reason, you lost, @:(message.dio).'
}
}
}
```

Template the below:

```html
<p>{{ $t('message.linked') }}</p>
```

Output the below:

```html
<p>There's a reason, you lost, Dio.</p>
```

0 comments on commit acfc458

Please sign in to comment.