-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlocale-messages.ts
337 lines (315 loc) · 9.36 KB
/
locale-messages.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @fileoverview Classes that acquires and manages localization messages
* @author Yosuke Ota
*/
import type { AST as VAST } from 'vue-eslint-parser'
import type {
RuleContext,
I18nLocaleMessageDictionary,
I18nLocaleMessageValue,
LocaleKeyType
} from '../types'
import { extname } from 'path'
import fs from 'fs'
import {
parseYamlValuesInI18nBlock,
parseJsonValuesInI18nBlock
} from './parsers'
import { ResourceLoader } from './resource-loader'
import JSON5 from 'json5'
import yaml from 'js-yaml'
import { joinPath, parsePath } from './key-path'
// see https://github.com/kazupon/vue-cli-plugin-i18n/blob/e9519235a454db52fdafcd0517ce6607821ef0b4/generator/templates/js/src/i18n.js#L10
const DEFAULT_LOCALE_PATTERN = '[A-Za-z0-9-_]+'
const DEFAULT_LOCALE_FIELNAME_REGEX = new RegExp(
`(${DEFAULT_LOCALE_PATTERN})\.`,
'i'
)
const DEFAULT_LOCALE_CAPTURE_REGEX = new RegExp(
`^.*\/(?<locale>${DEFAULT_LOCALE_PATTERN})\.(json5?|ya?ml)$`,
'i'
)
/**
* The localization message class
*/
export abstract class LocaleMessage {
public readonly fullpath: string
public readonly localeKey: LocaleKeyType
public readonly file: string
public readonly localePattern: RegExp
private _locales: string[] | undefined
/**
* @param {object} arg
* @param {string} arg.fullpath Absolute path.
* @param {string[]} [arg.locales] The locales.
* @param {LocaleKeyType} arg.localeKey Specifies how to determine the locale for localization messages.
* @param {RegExp} args.localePattern Specifies how to determin the regular expression pattern for how to get the locale.
*/
constructor({
fullpath,
locales,
localeKey,
localePattern
}: {
fullpath: string
locales?: string[]
localeKey: LocaleKeyType
localePattern?: string | RegExp
}) {
this.fullpath = fullpath
/** @type {LocaleKeyType} Specifies how to determine the locale for localization messages. */
this.localeKey = localeKey
/** @type {string} The localization messages file name. */
this.file = fullpath.replace(/^.*(\\|\/|:)/, '')
this.localePattern = this.getLocalePatternWithRegex(localePattern)
this._locales = locales
}
/**
* @protected
*/
abstract getMessagesInternal(): I18nLocaleMessageDictionary
/**
* Get locale pattern with regular expression
*/
getLocalePatternWithRegex(localePattern?: string | RegExp): RegExp {
// prettier-ignore
return localePattern != null
? typeof localePattern === 'string'
? new RegExp(localePattern, 'i')
: Object.prototype.toString.call(localePattern) === '[object RegExp]'
? localePattern
: DEFAULT_LOCALE_CAPTURE_REGEX
: DEFAULT_LOCALE_CAPTURE_REGEX
}
/**
* @returns {object} The localization messages object.
*/
get messages(): I18nLocaleMessageDictionary {
return this.getMessagesInternal()
}
/**
* @returns {string[]} Array of locales.
*/
get locales(): string[] {
if (this._locales) {
return this._locales
}
if (this.localeKey === 'file') {
const matched = this.file.match(DEFAULT_LOCALE_FIELNAME_REGEX)
return (this._locales = [(matched && matched[1]) || this.file])
} else if (this.localeKey === 'path') {
const matched = this.fullpath.match(this.localePattern)
return (this._locales = [
(matched && matched.groups?.locale) || this.fullpath
])
} else if (this.localeKey === 'key') {
return (this._locales = Object.keys(this.messages))
}
return (this._locales = [])
}
isResolvedLocaleByFileName() {
return this.localeKey === 'file' || this.localeKey === 'path'
}
/**
* Gets messages for the given locale.
*/
getMessagesFromLocale(locale: string): I18nLocaleMessageDictionary {
if (this.isResolvedLocaleByFileName()) {
if (!this.locales.includes(locale)) {
return {}
}
return this.messages
}
if (this.localeKey === 'key') {
return (this.messages[locale] || {}) as I18nLocaleMessageDictionary
}
return {}
}
}
export class BlockLocaleMessage extends LocaleMessage {
public readonly block: VAST.VElement
private lang: string
private _messages: I18nLocaleMessageDictionary | null = null
/**
* @param {object} arg
* @param {VElement} arg.block `<i18n>` block.
* @param {string} arg.fullpath Absolute path.
* @param {string[]} [arg.locales] The locales.
* @param {LocaleKeyType} arg.localeKey Specifies how to determine the locale for localization messages.
* @param {RuleContext} arg.context The rule context
* @param {string} arg.lang The language of resource
* @returns {LocaleMessage}
*/
constructor({
block,
fullpath,
locales,
localeKey,
lang = 'json'
}: {
block: VAST.VElement
fullpath: string
locales?: string[]
localeKey: LocaleKeyType
context: RuleContext
lang?: string
}) {
super({
fullpath,
locales,
localeKey
})
this.block = block
this.lang = lang || 'json'
}
getMessagesInternal(): I18nLocaleMessageDictionary {
if (this._messages) {
return this._messages
}
const { lang } = this
if (lang === 'json' || lang === 'json5') {
this._messages = parseJsonValuesInI18nBlock(this.block) || {}
} else if (lang === 'yaml' || lang === 'yml') {
this._messages = parseYamlValuesInI18nBlock(this.block) || {}
} else {
this._messages = {}
}
return this._messages
}
}
export class FileLocaleMessage extends LocaleMessage {
private _resource: ResourceLoader<I18nLocaleMessageDictionary>
/**
* @param {object} arg
* @param {string} arg.fullpath Absolute path.
* @param {string[]} [arg.locales] The locales.
* @param {LocaleKeyType} arg.localeKey Specifies how to determine the locale for localization messages.
* @param {string | RegExp} args.localePattern Specifies how to determin the regular expression pattern for how to get the locale.
*/
constructor({
fullpath,
locales,
localeKey,
localePattern
}: {
fullpath: string
locales?: string[]
localeKey: LocaleKeyType
localePattern?: string | RegExp
}) {
super({
fullpath,
locales,
localeKey,
localePattern
})
this._resource = new ResourceLoader(fullpath, fileName => {
const ext = extname(fileName).toLowerCase()
if (ext === '.js') {
const key = require.resolve(fileName)
delete require.cache[key]
return require(fileName)
} else if (ext === '.yaml' || ext === '.yml') {
return yaml.load(fs.readFileSync(fileName, 'utf8'))
} else if (ext === '.json' || ext === '.json5') {
return JSON5.parse(fs.readFileSync(fileName, 'utf8'))
}
})
}
getMessagesInternal(): I18nLocaleMessageDictionary {
return this._resource.getResource()
}
}
/**
* The localization messages class
*/
export class LocaleMessages {
public readonly localeMessages: LocaleMessage[]
/**
* @param {LocaleMessage[]} localeMessages
*/
constructor(localeMessages: LocaleMessage[]) {
this.localeMessages = localeMessages
}
get locales(): string[] {
const locales = new Set<string>()
for (const localeMessage of this.localeMessages) {
for (const locale of localeMessage.locales) {
locales.add(locale)
}
}
return [...locales]
}
/**
* Checks whether it is empty.
*/
isEmpty(): boolean {
return this.localeMessages.length <= 0
}
/**
* Finds and gets the localization message for the given fullpath.
* @param {string} fullpath
* @returns {LocaleMessage}
*/
findExistLocaleMessage(fullpath: string): LocaleMessage | null {
return (
this.localeMessages.find(message => message.fullpath === fullpath) || null
)
}
/**
* Finds and gets the localization message for the given block.
* @param {VElement} block
* @returns {BlockLocaleMessage}
*/
findBlockLocaleMessage(block: VAST.VElement): BlockLocaleMessage | null {
return (
this.localeMessages.find(
(message): message is BlockLocaleMessage =>
(message as BlockLocaleMessage).block === block
) || null
)
}
/**
* Finds the path that does not exist in the localization message resources.
* @param {string} key
*/
findMissingPath(key: string): string | null {
let missingPath: string[] = []
for (const locale of this.locales) {
const localeMessages: I18nLocaleMessageValue[] = this.localeMessages.map(
lm => lm.getMessagesFromLocale(locale)
)
if (
localeMessages.some(last => {
return last && typeof last === 'object' ? last[key] != null : false
})
) {
// Hit the original key.
return null
}
const paths = [...parsePath(key)]
let lasts = localeMessages
const targetPaths = []
while (paths.length) {
const path = paths.shift()!
targetPaths.push(path)
const values: I18nLocaleMessageValue[] = lasts
.map(last => {
return last && typeof last === 'object' ? last[path] : undefined
})
.filter((val): val is I18nLocaleMessageValue => val != null)
if (values.length === 0) {
if (missingPath.length <= targetPaths.length) {
missingPath = targetPaths
}
break
}
lasts = values
}
if (!missingPath.length) {
return null
}
}
return joinPath(...missingPath)
}
}