forked from minbrowser/min
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildLocalization.js
67 lines (47 loc) · 1.67 KB
/
buildLocalization.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
/* compile the language files into dist/localization.build.js */
const path = require('path')
const fs = require('fs')
const decomment = require('decomment')
const outputDir = path.join(__dirname, '../dist')
const outputFile = path.join(outputDir, 'localization.build.js')
const languageFileDir = path.join(__dirname, '../localization/languages')
function buildLocalization () {
// read all the files from the "languages" directory
const languageFiles = fs.readdirSync(languageFileDir)
// build languages object
const languages = {}
languageFiles.forEach(function (file) {
const data = fs.readFileSync(path.join(languageFileDir, file), 'utf-8')
let obj
let decommented = null
try {
decommented = decomment(data)
obj = JSON.parse(decommented)
} catch (e) {
console.error('parsing language file "' + file + '" failed.')
console.error(e.toString())
if (decommented !== null) {
const msg = e.message
const match = msg.match(/at position (\d+)/)
if (match !== null) {
const loc = parseInt(match[1])
console.info('"' + decommented.substring(loc - 40, loc + 40) + '"')
}
}
process.exit()
}
languages[obj.identifier] = obj
})
let fileContents = 'var languages = ' + JSON.stringify(languages) + ';\n'
// add contents of localization.js (helper functions, ...)
fileContents += fs.readFileSync(path.join(__dirname, '../localization/localizationHelpers.js'))
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}
fs.writeFileSync(outputFile, fileContents)
}
if (module.parent) {
module.exports = buildLocalization
} else {
buildLocalization()
}