forked from cap-collectif/cap-collectif
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_translations.mjs
More file actions
43 lines (37 loc) · 1.54 KB
/
Copy pathfetch_translations.mjs
File metadata and controls
43 lines (37 loc) · 1.54 KB
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
import fs from 'fs'
import fetch from 'node-fetch'
import chalk from 'chalk'
const LOCO_READ_ONLY_KEY = process.env.LOCO_READ_ONLY_KEY
const locales = ['fr-FR', 'es-ES', 'en-GB', 'de-DE', 'nl-NL', 'sv-SE', 'eu-EU', 'oc-OC', 'ur-IN']
const domains = ['CapcoAppBundle', 'SonataAdminBundle', 'messages']
const invalidKeyMessage = `"error": "Invalid project key"`
async function main() {
for (const locale of locales) {
console.log(`[${chalk.green(locale)}]: Downloading up to date xlf and JSON files…`)
await fetch(
`https://localise.biz:443/api/export/locale/${locale}.xlf?format=symfony&no-comments=true&key=${LOCO_READ_ONLY_KEY}`,
).then(res => {
for (const domain of domains) {
const xlf = fs.createWriteStream(`translations/${domain}+intl-icu.${locale}.xlf`)
res.body.pipe(xlf)
}
})
await fetch(
`https://localise.biz:443/api/export/locale/${locale}.json?&no-folding=true&no-comments=true&key=${LOCO_READ_ONLY_KEY}`,
)
.then(res => res.json())
.then(json => {
const dir = 'public/js'
const bundlePath = `${dir}/${locale}.js`
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
const messages = JSON.stringify(json, null, 2)
if (messages.includes(invalidKeyMessage)) throw invalidKeyMessage
fs.writeFileSync(`translations/${locale}.json`, messages)
fs.writeFileSync(bundlePath, `window.intl_messages=${messages};`)
fs.writeFileSync(`${dir}/${locale}-es6.js`, `export default ${messages};`)
})
}
}
main()