/
utils.ts
257 lines (238 loc) · 7.04 KB
/
utils.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
isArray,
isObject,
hasOwn,
isPlainObject,
isString,
warn
} from '@intlify/shared'
import { Text, createVNode } from 'vue'
import { I18nErrorCodes, createI18nError } from './errors'
import { I18nWarnCodes, getWarnMessage } from './warnings'
import type { Locale, MessageResolver } from '@intlify/core-base'
import type {
Composer,
ComposerOptions,
CustomBlocks,
VueMessageType
} from './composer'
import type {
ComponentInternalInstance,
RendererNode,
RendererElement
} from 'vue'
type GetLocaleMessagesOptions<Messages = {}> = {
messages?: { [K in keyof Messages]: Messages[K] }
__i18n?: CustomBlocks<VueMessageType>
messageResolver?: MessageResolver
flatJson?: boolean
}
declare module 'vue' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface VNode<HostNode = RendererNode, HostElement = RendererElement> {
toString: () => string // mark for vue-i18n message runtime
}
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function isLegacyVueI18n(VueI18n: any): boolean {
if (VueI18n == null || VueI18n.version == null) {
return false
}
return (Number(VueI18n.version.split('.')[0]) || -1) >= 8
}
/**
* Transform flat json in obj to normal json in obj
*/
export function handleFlatJson(obj: unknown): unknown {
// check obj
if (!isObject(obj)) {
return obj
}
for (const key in obj as object) {
// check key
if (!hasOwn(obj, key)) {
continue
}
// handle for normal json
if (!key.includes('.')) {
// recursive process value if value is also a object
if (isObject(obj[key])) {
handleFlatJson(obj[key])
}
}
// handle for flat json, transform to normal json
else {
// go to the last object
const subKeys = key.split('.')
const lastIndex = subKeys.length - 1
let currentObj = obj
let hasStringValue = false
for (let i = 0; i < lastIndex; i++) {
if (!(subKeys[i] in currentObj)) {
currentObj[subKeys[i]] = {}
}
if (!isObject(currentObj[subKeys[i]])) {
__DEV__ &&
warn(
getWarnMessage(I18nWarnCodes.IGNORE_OBJ_FLATTEN, {
key: subKeys[i]
})
)
hasStringValue = true
break
}
currentObj = currentObj[subKeys[i]]
}
// update last object value, delete old property
if (!hasStringValue) {
currentObj[subKeys[lastIndex]] = obj[key]
delete obj[key]
}
// recursive process value if value is also a object
if (isObject(currentObj[subKeys[lastIndex]])) {
handleFlatJson(currentObj[subKeys[lastIndex]])
}
}
}
return obj
}
export function getLocaleMessages<Messages = {}>(
locale: Locale,
options: GetLocaleMessagesOptions<Messages>
): { [K in keyof Messages]: Messages[K] } {
const { messages, __i18n, messageResolver, flatJson } = options
// prettier-ignore
const ret = (isPlainObject(messages)
? messages
: isArray(__i18n)
? {}
: { [locale]: {} } )as Record<string, any>
// merge locale messages of i18n custom block
if (isArray(__i18n)) {
__i18n.forEach(custom => {
if ('locale' in custom && 'resource' in custom) {
const { locale, resource } = custom
if (locale) {
ret[locale] = ret[locale] || {}
deepCopy(resource, ret[locale])
} else {
deepCopy(resource, ret)
}
} else {
isString(custom) && deepCopy(JSON.parse(custom), ret)
}
})
}
// handle messages for flat json
if (messageResolver == null && flatJson) {
for (const key in ret) {
if (hasOwn(ret, key)) {
handleFlatJson(ret[key])
}
}
}
return ret as { [K in keyof Messages]: Messages[K] }
}
const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val)
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function deepCopy(src: any, des: any): void {
// src and des should both be objects, and non of then can be a array
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
throw createI18nError(I18nErrorCodes.INVALID_VALUE)
}
for (const key in src) {
if (hasOwn(src, key)) {
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
// replace with src[key] when:
// src[key] or des[key] is not a object, or
// src[key] or des[key] is a array
des[key] = src[key]
} else {
// src[key] and des[key] are both object, merge them
deepCopy(src[key], des[key])
}
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getComponentOptions(instance: ComponentInternalInstance): any {
return !__BRIDGE__ ? instance.type : instance.proxy!.$options
}
export function adjustI18nResources(
gl: Composer,
options: ComposerOptions,
componentOptions: any // eslint-disable-line @typescript-eslint/no-explicit-any
): void {
let messages = isObject(options.messages) ? options.messages : {}
if ('__i18nGlobal' in componentOptions) {
messages = getLocaleMessages(gl.locale.value as Locale, {
messages,
__i18n: componentOptions.__i18nGlobal
})
}
// merge locale messages
const locales = Object.keys(messages)
if (locales.length) {
locales.forEach(locale => {
gl.mergeLocaleMessage(locale, messages[locale])
})
}
if (!__LITE__) {
// merge datetime formats
if (isObject(options.datetimeFormats)) {
const locales = Object.keys(options.datetimeFormats)
if (locales.length) {
locales.forEach(locale => {
gl.mergeDateTimeFormat(locale, options.datetimeFormats![locale])
})
}
}
// merge number formats
if (isObject(options.numberFormats)) {
const locales = Object.keys(options.numberFormats)
if (locales.length) {
locales.forEach(locale => {
gl.mergeNumberFormat(locale, options.numberFormats![locale])
})
}
}
}
}
export function createTextNode(key: string): any {
return !__BRIDGE__
? createVNode(Text, null, key, 0)
: createVNodeVue2Compatible(key)
}
function createVNodeVue2Compatible(key: string): any {
// shim Vue2 VNode interface
// see the https://github.com/vuejs/vue/blob/dev/src/core/vdom/vnode.js
const componentInstance = undefined
return {
tag: undefined,
data: undefined,
children: undefined,
text: key,
elm: undefined,
ns: undefined,
context: undefined,
fnContext: undefined,
fnOptions: undefined,
fnScopeId: undefined,
key: undefined,
componentOptions: undefined,
componentInstance,
parent: undefined,
raw: false,
isStatic: false,
isRootInsert: true,
isComment: false,
isCloned: false,
isOnce: false,
asyncFactory: undefined,
asyncMeta: undefined,
isAsyncPlaceholder: false,
child: () => componentInstance
}
}
/* eslint-enable @typescript-eslint/no-explicit-any */