-
Notifications
You must be signed in to change notification settings - Fork 16
/
generate.js
72 lines (58 loc) · 2.31 KB
/
generate.js
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
const fs = require('fs-extra')
const path = require('path')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const { gettextToI18next } = require('i18next-conv')
const handlebars = require('handlebars')
const { checkDirectoryExists } = require('./helpers')
const { langToLocale } = require('./locales')
const writeTemplate = (outFile, data) => {
const localesHBS = fs.readFileSync(
path.join(__dirname, 'templates', 'locales.hbs'),
'utf8'
)
const localesTemplate = handlebars.compile(localesHBS)
fs.writeFileSync(outFile, localesTemplate(data))
}
const generate = async ({ input, output, namespace }) => {
if (!checkDirectoryExists(input)) {
const relativeInput = './' + path.relative(process.cwd(), input)
reporter.debug(
`Source directory ${chalk.bold(
relativeInput
)} does not exist, skipping i18n generation`
)
return false
}
// clean-up and create destination dir.
const dst = path.normalize(output)
fs.removeSync(dst)
fs.ensureDirSync(dst)
reporter.debug(`[i18n-generate] Reading translation sources...`)
const files = fs.readdirSync(input)
const langs = files.map(f => path.basename(f, path.extname(f)))
const locales = langs
.filter(lang => lang !== 'en')
.map(lang => langToLocale[lang])
const outFile = path.join(dst, 'index.js')
writeTemplate(outFile, { locales, langs, namespace })
reporter.debug(`[i18n-generate] Generating translation .json files...`)
const promises = files.map(async f => {
const ext = path.extname(f)
const lang = path.basename(f, ext)
reporter.debug(
`[i18n-generate] Writing JSON translation file for language: ${lang}...`
)
if (ext === '.po' || ext === '.pot') {
const filePath = path.join(input, f)
const contents = fs.readFileSync(filePath, 'utf8')
const json = await gettextToI18next(lang, contents)
const target = path.join(dst, lang)
fs.ensureDirSync(target)
const translationsPath = path.join(target, 'translations.json')
fs.writeFileSync(translationsPath, json, { encoding: 'utf8' })
}
})
await Promise.all(promises)
return true
}
module.exports = generate