This repository has been archived by the owner on Aug 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (90 loc) · 2.74 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
107
108
109
110
'use strict';
var fs = require('fs'),
env = require('node-env-file'),
request = require('superagent'),
btoa = require('btoa')
var args = process.argv.slice(2)
var baseAPI = 'https://www.transifex.com/api/2/',
outputPath = __dirname + '/output/'
var envFile = __dirname + '/.env'
try {
env(envFile)
} catch (err) {
// Nothing. If env file not found, move on.
}
if (!process.env.TRANSIFEX_USERNAME || !process.env.TRANSIFEX_PASSWORD) {
console.error('Transifex authorization credentials are missing.')
return
}
var token = btoa(process.env.TRANSIFEX_USERNAME + ':' + process.env.TRANSIFEX_PASSWORD)
var languageAPI = baseAPI + 'project/streetmix/languages/'
var resourceSlug = 'main'
// If arguments given, process those instead of all
if (args && args.length > 0) {
for (var i = 0; i < args.length; i++) {
getTranslation(args[i])
}
return
}
// Get the list of all available languages from Transifex
console.log('Retrieving all languages from Transifex...')
request
.get(languageAPI)
.set('Authorization', 'Basic ' + token)
.end(function (err, res) {
if (err) {
console.log(err)
return
}
if (res.error) {
console.log(res.error)
return
}
var languages = res.body
// Note: this automatically skips en-US, but you can manually request
// it in arguments, if you want it.
if (languages.length === 0) {
console.log('No languages retrieved.')
return
}
for (var j = 0; j < languages.length; j++) {
getTranslation(languages[j].language_code)
}
})
function getTranslation (locale) {
var translationAPI = baseAPI + 'project/streetmix/resource/' + resourceSlug + '/translation/' + locale + '/'
// path for Streetmix front-end
var localePath = locale.replace(/@|_/, '-')
request
.get(translationAPI)
.set('Authorization', 'Basic ' + token)
.set('Accept', 'text/xml')
.end(function (err, res) {
if (err) {
console.log(err)
return
}
if (res.error) {
if (res.error.status === 404) {
console.log('No translation exists for locale `' + locale + '`.')
} else {
console.log(res.error)
}
return
}
var translation = JSON.parse(res.body.content)
// Create the directory, skip error if it exists
try {
fs.mkdirSync(outputPath + localePath)
} catch (err) {
if (err.code !== 'EEXIST') {
console.log(err)
}
}
// Write translation file
fs.writeFile(outputPath + localePath + '/translation.json', JSON.stringify(translation, null, 2), function (err) {
if (err) throw err
console.log('Retrieved translation file for locale `' + locale + '`.')
})
})
}