chore: add a script to cleanup old translations (#1963)
This commit is contained in:
parent
dac044e6ad
commit
2267556b8b
13 changed files with 68 additions and 91 deletions
65
scripts/cleanup-translations.ts
Normal file
65
scripts/cleanup-translations.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import flatten from 'flat'
|
||||
import { createResolver } from '@nuxt/kit'
|
||||
import fs from 'fs-extra'
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sourceFiles: string[] = sourceLanguageLocale.files ? sourceLanguageLocale.files : [sourceLanguageLocale.file!]
|
||||
|
||||
const sourceTranslations: Record<string, string> = {}
|
||||
|
||||
for (const file of sourceFiles) {
|
||||
const data = JSON.parse(Buffer.from(
|
||||
await fs.readFile(resolver.resolve(`../locales/${file}`), 'utf-8'),
|
||||
).toString()) as Record<string, unknown>
|
||||
|
||||
merge(flatten(data), sourceTranslations)
|
||||
}
|
||||
|
||||
async function removeOutdatedTranslations() {
|
||||
for (const locale of currentLocales.filter(l => l.code !== 'en-US')) {
|
||||
const files: string[] = locale.files ? locale.files : [locale.file!]
|
||||
|
||||
for (const file of files) {
|
||||
const path = resolver.resolve(`../locales/${file}`)
|
||||
|
||||
const data = JSON.parse(Buffer.from(
|
||||
await fs.readFile(path, 'utf-8'),
|
||||
).toString())
|
||||
|
||||
const targetTranslations: Record<string, string> = flatten(data)
|
||||
|
||||
for (const key in targetTranslations) {
|
||||
if (!sourceTranslations[key])
|
||||
delete targetTranslations[key]
|
||||
}
|
||||
|
||||
const unflattened = flatten.unflatten(targetTranslations)
|
||||
|
||||
await fs.writeFile(
|
||||
path,
|
||||
`${JSON.stringify(unflattened, null, 2)}\n`,
|
||||
{ encoding: 'utf-8' },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
removeOutdatedTranslations()
|
Loading…
Add table
Add a link
Reference in a new issue