UNPKG

1.47 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const glob = require('glob')
4const util = require('util')
5const mkdirp = require('mkdirp')
6const rimraf = require('rimraf')
7const path = require('path')
8const fs = require('fs')
9const murmurHash = require('babel-plugin-react-intl-id-hash/lib/murmurHash')
10 .default
11
12const readFile = util.promisify(fs.readFile)
13const writeFile = util.promisify(fs.writeFile)
14const mkdirpPromise = util.promisify(mkdirp)
15
16async function readJson(file) {
17 return JSON.parse(await readFile(file))
18}
19
20async function writeJson(file, data) {
21 await mkdirpPromise(path.dirname(file))
22 await writeFile(file, JSON.stringify(data, null, 2))
23}
24
25rimraf.sync('translations/*')
26
27// "shared" strings are included in translations for all components
28glob('messages/**/*.json', async function(er, files) {
29 const allMsgs = {}
30 for (const file of files) {
31 const lang = path.basename(file)
32 const msgs = await readJson(file)
33 if (!allMsgs[lang]) allMsgs[lang] = msgs
34 else allMsgs[lang] = { ...allMsgs[lang], ...msgs }
35 }
36 for (const lang in allMsgs) {
37 const translations = {}
38 const msgs = allMsgs[lang]
39 Object.keys(msgs).forEach(key => {
40 // For production message ids are hashed, so we need to hash the ids of
41 // translations too
42 const hashedKey = murmurHash(key)
43 translations[hashedKey] = msgs[key].message
44 })
45 const output = path.join(__dirname, '../translations', lang)
46 await writeJson(output, translations)
47 }
48})