-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreorgIntlFiles.ts
54 lines (43 loc) · 1.59 KB
/
reorgIntlFiles.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
import fs from "fs"
import os from "os"
import path from "path"
import walkdir from "walkdir"
import _ from "lodash"
import { defaultLanguage, supportedLanguages } from "../utils/languages"
const sourcePath = "src/intl"
async function reorgIntl() {
const defaultLangTranslations: Record<string, Record<string, string>> = {}
const allTranslationsPerLang: Record<string, Record<string, string>> = {}
supportedLanguages.forEach((language) => {
allTranslationsPerLang[language] = {}
// load translations per language in memory
walkdir.sync(path.join(sourcePath, language), (filepath) => {
const data = fs.readFileSync(filepath, { encoding: "utf8" })
const translations = JSON.parse(data) as Record<string, string>
if (language === defaultLanguage) {
const namespace = path.basename(filepath)
defaultLangTranslations[namespace] = translations
} else {
allTranslationsPerLang[language] = {
...allTranslationsPerLang[language],
...translations,
}
}
})
})
supportedLanguages.forEach((language) => {
const allTranslations = allTranslationsPerLang[language]
Object.keys(defaultLangTranslations).forEach((ns) => {
const defaultLangContent = defaultLangTranslations[ns]
const defaultLangKeys = Object.keys(defaultLangContent)
const translations = _.pick(allTranslations, defaultLangKeys)
if (!_.isEmpty(translations)) {
fs.writeFileSync(
path.join(sourcePath, language, ns),
JSON.stringify(translations, null, 2) + os.EOL
)
}
})
})
}
reorgIntl()