overleaf/services/web/scripts/translations/transformLocales.js
Liangjun Song 8293771f58 Merge pull request #21290 from overleaf/ls-scripts-to-esm-translations
Migrate scripts/translation to esm

GitOrigin-RevId: 475ec949f0ba238791df91de109169584e68c701
2024-10-25 08:06:12 +00:00

54 lines
1.1 KiB
JavaScript

import Path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'node:url'
import { loadLocale } from './utils.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const LANGUAGES = [
'cs',
'da',
'de',
'en',
'es',
'fi',
'fr',
'it',
'ja',
'ko',
'nl',
'no',
'pl',
'pt',
'ru',
'sv',
'tr',
'zh-CN',
]
const LOCALES = {}
LANGUAGES.forEach(loadLocales)
function loadLocales(language) {
LOCALES[language] = loadLocale(language)
}
function transformLocales(mapping, transformLocale) {
Object.entries(LOCALES).forEach(([language, translatedLocales]) => {
Object.entries(mapping).forEach(([localeKey, spec]) => {
const locale = translatedLocales[localeKey]
if (!locale) {
// This locale is not translated yet.
return
}
translatedLocales[localeKey] = transformLocale(locale, spec)
})
fs.writeFileSync(
Path.join(__dirname, `/../../locales/${language}.json`),
JSON.stringify(translatedLocales, null, 2) + '\n'
)
})
}
export default {
transformLocales,
}