-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateLocales.ts
75 lines (60 loc) · 2.24 KB
/
createLocales.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fs from "fs"
import path from "path"
import walkdir from "walkdir"
import _ from "lodash"
import rimraf from "rimraf"
import { defaultLanguage, supportedLanguages } from "../utils/languages"
const input = "src/intl"
const output = "i18n/locales"
/**
* Creates the locales used later by the app.
*
* 1. Loads all the `defaultLang` translations under `input`
* 2. For each language and namespace, it will create a locale under `output`
* 3. For any missing translation, it will fill that string with the
* `defaultLang` version of it
*/
async function createLocales(): Promise<void> {
// cleanup any previous generated locales
await rimraf(output)
const defaultLangLocales: Record<string, Record<string, string>> = {}
walkdir.sync(`${input}/${defaultLanguage}/`, (filepath) => {
const namespace = path.basename(filepath)
// load english content per ns in memory in order to compare it with the
// other languages
const data = fs.readFileSync(filepath, { encoding: "utf8" })
const translations = JSON.parse(data)
defaultLangLocales[namespace] = translations
})
supportedLanguages.forEach((language) => {
//create language folder in `output`
fs.mkdirSync(path.join(output, language), { recursive: true })
const nsInLanguage: Array<string> = []
walkdir.sync(path.join(input, language), (filepath) => {
const ns = path.basename(filepath)
nsInLanguage.push(ns)
const data = fs.readFileSync(filepath, { encoding: "utf8" })
const translations = JSON.parse(data)
// check differences and fill the missing translations strings in english
const defaultLangContent = defaultLangLocales[ns]
const completeSet = { ...defaultLangContent, ...translations }
fs.writeFileSync(
path.join(output, language, ns),
JSON.stringify(completeSet, null, 2)
)
})
const missingNs = _.difference(
Object.keys(defaultLangLocales),
nsInLanguage
)
missingNs.forEach((ns) => {
// fill the content with english content
const defaultLangContent = defaultLangLocales[ns]
fs.writeFileSync(
path.join(output, language, ns),
JSON.stringify(defaultLangContent, null, 2)
)
})
})
}
export default createLocales