Add Omorphia locale files loading (#1447)

Implements loading of all locale files shipped with Omorphia package by
globbing and registering them during the `vintl:extendOptions` hook.

Also updated VIntl for Nuxt to ensure those files are properly loaded.
Previous version of VIntl for Nuxt had a somewhat broken module ID
tracking, preventing Omorphia locale files to be properly transformed
during the build; at least on systems like Windows, don't know about
*nix. The tracking approach was replaced in 1.8.0, and it seems to be
working now.
This commit is contained in:
Sasha Sorokin 2023-12-05 13:48:12 +11:00 committed by GitHub
parent accc53c5dd
commit 6d70ced93a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 221 additions and 469 deletions

View File

@ -186,7 +186,6 @@ export default defineNuxtConfig({
const resolveCompactNumberDataImport = await (async () => {
const compactNumberLocales: string[] = []
const resolvedImports = new Map<string, string>()
for await (const localeFile of globIterate(
'node_modules/@vintl/compact-number/dist/locale-data/*.mjs',
@ -194,7 +193,6 @@ export default defineNuxtConfig({
)) {
const tag = basename(localeFile, '.mjs')
compactNumberLocales.push(tag)
resolvedImports.set(tag, String(pathToFileURL(resolve(localeFile))))
}
function resolveImport(tag: string) {
@ -207,6 +205,33 @@ export default defineNuxtConfig({
return resolveImport
})()
const resolveOmorphiaLocaleImport = await (async () => {
const omorphiaLocales: string[] = []
const omorphiaLocaleSets = new Map<string, { files: { from: string }[] }>()
for await (const localeDir of globIterate('node_modules/omorphia/locales/*', {
posix: true,
})) {
const tag = basename(localeDir)
omorphiaLocales.push(tag)
const localeFiles: { from: string; format?: string }[] = []
omorphiaLocaleSets.set(tag, { files: localeFiles })
for await (const localeFile of globIterate(`${localeDir}/*`, { posix: true })) {
localeFiles.push({
from: pathToFileURL(localeFile).toString(),
format: 'default',
})
}
}
return function resolveLocaleImport(tag: string) {
return omorphiaLocaleSets.get(matchLocale([tag], omorphiaLocales, 'en-x-placeholder'))
}
})()
for await (const localeDir of globIterate('locales/*/', { posix: true })) {
const tag = basename(localeDir)
if (isProduction && !enabledLocales.includes(tag) && opts.defaultLocale !== tag) continue
@ -215,20 +240,15 @@ export default defineNuxtConfig({
opts.locales.find((locale) => locale.tag === tag) ??
opts.locales[opts.locales.push({ tag }) - 1]
const localeFiles = (locale.files ??= [])
for await (const localeFile of globIterate(`${localeDir}/*`, { posix: true })) {
const fileName = basename(localeFile)
if (fileName === 'index.json') {
if (locale.file == null) {
locale.file = {
from: `./${localeFile}`,
format: 'crowdin',
}
} else {
;(locale.files ??= []).push({
from: `./${localeFile}`,
format: 'crowdin',
})
}
localeFiles.push({
from: `./${localeFile}`,
format: 'crowdin',
})
} else if (fileName === 'meta.json') {
const meta: Record<string, { message: string }> = await fs
.readFile(localeFile, 'utf8')
@ -247,6 +267,11 @@ export default defineNuxtConfig({
;(locale.meta ??= {}).category = categoryOverride
}
const omorphiaLocaleData = resolveOmorphiaLocaleImport(tag)
if (omorphiaLocaleData != null) {
localeFiles.push(...omorphiaLocaleData.files)
}
const cnDataImport = resolveCompactNumberDataImport(tag)
if (cnDataImport != null) {
;(locale.additionalImports ??= []).push({

View File

@ -21,7 +21,7 @@
"@typescript-eslint/parser": "^5.59.8",
"@vintl/compact-number": "^2.0.5",
"@vintl/how-ago": "^3.0.1",
"@vintl/nuxt": "^1.7.0",
"@vintl/nuxt": "^1.8.0",
"eslint": "^8.41.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",

637
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff