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

Commit 2748eb4

Browse files
jvmccarthykazupon
authored andcommitted
⭐ new(config): custom message formatter (#57) by @jvmccarthy
Allow for register a custom message formatter function with `Vue.config.i18nFormatter` for more flexibility in formatting messages.
1 parent 9586f71 commit 2748eb4

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

gitbook/api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646
}
4747
```
4848

49+
### i18nFormatter
50+
51+
- **Type:** `Function`
52+
53+
- **Default:** `null`
54+
55+
- **Usage:**
56+
57+
Assign a customer message formatter.
58+
59+
```javascript
60+
Vue.config.i18nFormatter = function(string, ...arguments) {
61+
//...
62+
//return formattedString;
63+
}
64+
```
65+
4966

5067
## Global Methods
5168

gitbook/formatting.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,14 @@ Output the following:
104104
```html
105105
<p>hello world</p>
106106
```
107+
108+
## Registering a custom formatter
109+
110+
If the provided formatter doesn't meet your needs, you can also register a custom formatter,
111+
112+
```javascript
113+
Vue.config.i18nFormatter = function(string, ...arguments) {
114+
//...
115+
//return formattedString;
116+
}
117+
```

src/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getWatcher, getDep } from './observer'
22

33
let fallback // fallback lang
44
let missingHandler = null // missing handler
5+
let i18nFormatter = null
56

67
export default function (Vue, langVM, lang) {
78
const { bind } = Vue.util
@@ -44,4 +45,12 @@ export default function (Vue, langVM, lang) {
4445
get: () => { return missingHandler },
4546
set: val => { missingHandler = val }
4647
})
48+
49+
// define Vue.config.i18Formatter configration
50+
Object.defineProperty(Vue.config, 'i18nFormatter', {
51+
enumerable: true,
52+
configurable: true,
53+
get: () => { return i18nFormatter },
54+
set: val => { i18nFormatter = val }
55+
})
4756
}

src/extend.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export default function (Vue) {
6060
}
6161
}
6262

63-
return args ? format(val, args) : val
63+
if (!args) {
64+
return val
65+
}
66+
return Vue.config.i18nFormatter ? Vue.config.i18nFormatter.apply(null, [val].concat(args)) : format(val, args)
6467
}
6568

6669
function translate (getter, lang, fallback, key, params) {

test/specs/fixture/locales.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export default {
2929
errors: [
3030
'this is 0 error code message',
3131
{
32-
internal1: 'this is internal 1 error message'
32+
internal1: 'this is internal 1 error message'
3333
},
3434
[
35-
'this is nested array error 1'
35+
'this is nested array error 1'
3636
]
3737
]
3838
},
@@ -58,10 +58,10 @@ export default {
5858
errors: [
5959
'これはエラーコード0のエラーメッセージです。',
6060
{
61-
internal1: 'これは内部エラーコード1のエラーメッセージです。'
61+
internal1: 'これは内部エラーコード1のエラーメッセージです。'
6262
},
6363
[
64-
'これはネストされた配列のエラー1です。'
64+
'これはネストされた配列のエラー1です。'
6565
]
6666
]
6767
}

test/specs/format_custom.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import assert from 'power-assert'
2+
import Vue from 'vue'
3+
import locales from './fixture/locales'
4+
5+
describe('custom formatter', () => {
6+
before(done => {
7+
Object.keys(locales).forEach(lang => {
8+
Vue.locale(lang, locales[lang])
9+
})
10+
Vue.config.lang = 'en'
11+
Vue.nextTick(done)
12+
})
13+
14+
after(done => {
15+
Vue.config.i18nFormatter = null
16+
Vue.nextTick(done)
17+
})
18+
19+
describe('global', () => {
20+
it('allows for specifying a custom formatter', done => {
21+
Vue.config.i18nFormatter = (string, ...args) => {
22+
assert.equal('the world', string)
23+
assert.equal(1, args[0])
24+
assert.equal('two', args[1])
25+
assert.deepEqual({ name: 'joe' }, args[2])
26+
done()
27+
}
28+
29+
Vue.t('message.hello', 1, 'two', { name: 'joe' })
30+
})
31+
})
32+
33+
describe('instance', () => {
34+
it('allows for specifying a custom formatter', done => {
35+
const vm = new Vue()
36+
Vue.config.i18nFormatter = (string, ...args) => {
37+
assert.equal('the world', string)
38+
assert.equal(1, args[0])
39+
assert.equal(2, args[1])
40+
assert.equal(3, args[2])
41+
done()
42+
}
43+
44+
vm.$t('message.hello', [1, 2, 3])
45+
})
46+
})
47+
})

test/specs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ require('./i18n')
1111
require('./asset')
1212
require('./component')
1313
require('./missing')
14+
require('./format_custom')
1415
require('./issues')

0 commit comments

Comments
 (0)