1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| /* @flow */
|
| import { warn, isString, isObject, includes, numberFormatKeys } from '../util'
|
| export default {
| name: 'i18n-n',
| functional: true,
| props: {
| tag: {
| type: [String, Boolean, Object],
| default: 'span'
| },
| value: {
| type: Number,
| required: true
| },
| format: {
| type: [String, Object]
| },
| locale: {
| type: String
| }
| },
| render (h: Function, { props, parent, data }: Object) {
| const i18n = parent.$i18n
|
| if (!i18n) {
| if (process.env.NODE_ENV !== 'production') {
| warn('Cannot find VueI18n instance!')
| }
| return null
| }
|
| let key: ?string = null
| let options: ?NumberFormatOptions = null
|
| if (isString(props.format)) {
| key = props.format
| } else if (isObject(props.format)) {
| if (props.format.key) {
| key = props.format.key
| }
|
| // Filter out number format options only
| options = Object.keys(props.format).reduce((acc, prop) => {
| if (includes(numberFormatKeys, prop)) {
| return Object.assign({}, acc, { [prop]: props.format[prop] })
| }
| return acc
| }, null)
| }
|
| const locale: Locale = props.locale || i18n.locale
| const parts: NumberFormatToPartsResult = i18n._ntp(props.value, locale, key, options)
|
| const values = parts.map((part, index) => {
| const slot: ?Function = data.scopedSlots && data.scopedSlots[part.type]
| return slot ? slot({ [part.type]: part.value, index, parts }) : part.value
| })
|
| const tag = (!!props.tag && props.tag !== true) || props.tag === false ? props.tag : 'span'
| return tag
| ? h(tag, {
| attrs: data.attrs,
| 'class': data['class'],
| staticClass: data.staticClass
| }, values)
| : values
| }
| }
|
|