2023-04-16 22:25:55 +03:00
|
|
|
import { Buffer } from 'node:buffer'
|
2024-09-11 17:37:59 +03:00
|
|
|
import { readFile, writeFile } from 'node:fs/promises'
|
2023-04-16 15:28:45 +03:00
|
|
|
import { createResolver } from '@nuxt/kit'
|
2024-09-30 11:11:56 +03:00
|
|
|
import { flatten, unflatten } from 'flat'
|
2023-04-16 15:28:45 +03:00
|
|
|
import { currentLocales } from '../config/i18n'
|
|
|
|
|
|
|
|
const resolver = createResolver(import.meta.url)
|
|
|
|
|
|
|
|
const sourceLanguageLocale = currentLocales.find(l => l.code === 'en-US')!
|
|
|
|
|
|
|
|
function merge(src: Record<string, any>, dst: Record<string, any>) {
|
|
|
|
for (const key in src) {
|
|
|
|
if (typeof src[key] === 'object') {
|
|
|
|
if (!dst[key])
|
|
|
|
dst[key] = {}
|
|
|
|
|
|
|
|
merge(src[key], dst[key])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dst[key] = src[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 19:46:14 +03:00
|
|
|
const sourceFiles = sourceLanguageLocale.files ? sourceLanguageLocale.files : [sourceLanguageLocale.file!]
|
2023-04-16 15:28:45 +03:00
|
|
|
const sourceTranslations: Record<string, string> = {}
|
|
|
|
|
|
|
|
for (const file of sourceFiles) {
|
|
|
|
const data = JSON.parse(Buffer.from(
|
2024-09-11 17:37:59 +03:00
|
|
|
await readFile(resolver.resolve(`../locales/${file as string}`), 'utf-8'),
|
2023-04-16 15:28:45 +03:00
|
|
|
).toString()) as Record<string, unknown>
|
|
|
|
|
|
|
|
merge(flatten(data), sourceTranslations)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeOutdatedTranslations() {
|
|
|
|
for (const locale of currentLocales.filter(l => l.code !== 'en-US')) {
|
2024-02-24 19:46:14 +03:00
|
|
|
const files = locale.files ? locale.files as string[] : [locale.file as string]
|
2023-04-16 15:28:45 +03:00
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
const path = resolver.resolve(`../locales/${file}`)
|
|
|
|
|
|
|
|
const data = JSON.parse(Buffer.from(
|
2024-09-11 17:37:59 +03:00
|
|
|
await readFile(path, 'utf-8'),
|
2023-04-16 15:28:45 +03:00
|
|
|
).toString())
|
|
|
|
|
|
|
|
const targetTranslations: Record<string, string> = flatten(data)
|
|
|
|
|
|
|
|
for (const key in targetTranslations) {
|
|
|
|
if (!sourceTranslations[key])
|
|
|
|
delete targetTranslations[key]
|
|
|
|
}
|
|
|
|
|
2024-03-21 15:13:28 +03:00
|
|
|
const unflattened = unflatten(targetTranslations)
|
2023-04-16 15:28:45 +03:00
|
|
|
|
2024-09-11 17:37:59 +03:00
|
|
|
await writeFile(
|
2023-04-16 15:28:45 +03:00
|
|
|
path,
|
|
|
|
`${JSON.stringify(unflattened, null, 2)}\n`,
|
|
|
|
{ encoding: 'utf-8' },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeOutdatedTranslations()
|