This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·106 lines (96 loc) · 3.05 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const parser = require('xml-mapping')
const pkg = require('./package.json')
const encoding = 'utf8'
const inputFile = process.argv.slice(2)[0]
const createJson = false
const replaces = {
// '\\w': 'test',
// '\n<x id="LINE_BREAK" ctype="lb"\/>': '<br>',
'!!!': '!!'
}
// simple custom log
const log = (...stuff) => console.log(pkg.name, stuff.join(''))
// read a file from disk
const read = filepath => new Promise((resolve, reject) => {
log('reading : ', filepath)
fs.readFile(filepath, encoding, (err, content) => {
if (err) reject(err)
else resolve(content)
})
})
// write file to disk
const write = (filepath, content) => new Promise((resolve, reject) => {
log('writing : ', filepath)
fs.writeFile(filepath, content, encoding, (err) => {
if (err) reject(err)
else resolve('success')
})
})
// fill a string template with data inside object
const fill = (template, data) => {
let content = template
// log('using data :', JSON.stringify(data, null, 4))
Object.keys(data).forEach(key => {
content = content.replace(key, data[key])
})
return content
}
// read text node properly
const readTextNode = (input) => {
let str = (input.join ? input.join('<br>') : input) + '' // if there is multiple translation lines, we get an array
// log('reading text node : ' + str)
Object.keys(replaces).forEach(key => {
str = str.replace(new RegExp(key, 'gi'), replaces[key])
})
return str
}
// return a promise rejection
const error = (str) => new Promise.reject(str)
// convert .xlf file to .po file
const convert = (filepath) => {
log('starting...')
read(filepath)
.then(xml => parser.load(xml))
.then(obj => {
const input = obj.xliff.file
const pretty = JSON.stringify(input, null, 4)
if (createJson) {
write('sample.json', pretty)
}
return read(path.join(__dirname, 'template.po')).then(template => {
const name = inputFile.split('.')[0]
const lang = input['source-language']
const data = {
PROJECT_NAME: name,
GENERATOR_NAME: pkg.name,
GENERATOR_VERSION: pkg.version,
LANGUAGE_CODE: lang
}
let content = fill(template, data)
const translations = input.body['trans-unit']
if (!translations.length) {
return error('no translations found in xlf')
}
translations.forEach(translation => {
content += '#: ' + translation['context-group'].context[0].$t + '\n'
content += 'msgid "' + translation.id + '"\n'
content += 'msgstr "' + readTextNode(translation.source.$t) + '"\n\n'
})
log('processed : ' + translations.length + ' translations')
return write(name + '.' + lang + '.po', content)
})
})
.then(status => {
log('finnished')
log('status : ', status)
})
}
// init
if (!inputFile) {
log('miss argument, please specify xlf translation input file like :\nxliff2po path/to/my/translation-file.xlf')
} else {
convert(inputFile)
}