Created
October 7, 2017 15:19
-
-
Save M-ZubairAhmed/98ada20ce17956ef45228c6f1f0e8e1a to your computer and use it in GitHub Desktop.
Extracting all strings from react-intl to single json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Extracted all strings from program ', '\n') | |
import * as fs from 'fs' | |
import { sync as globSync } from 'glob' | |
import { sync as mkdirpSync } from 'mkdirp' | |
const filePattern = './build/messages/**/*.json' | |
const outputLanguageDataDir = './build/locales/' | |
console.log('Piping followings ids :') | |
const defaultMessages = globSync(filePattern) | |
.map(filename => fs.readFileSync(filename, 'utf8')) | |
.map(file => JSON.parse(file)) | |
.reduce((collection, descriptors) => { | |
descriptors.forEach(({ id, defaultMessage }) => { | |
if (collection.hasOwnProperty(id)) { | |
throw new Error(`Duplicate message id: ${id}`) | |
console.log(`*** ERROR: Duplicate ID found: ${id} ***`.bold.red) | |
console.log(`Look in file ${filename} to find duplicate`.red) | |
} | |
collection[id] = defaultMessage | |
console.log('-', id) | |
}) | |
return collection | |
}, {}) | |
mkdirpSync(outputLanguageDataDir) | |
console.log('\n', 'Inserting all ids into single file', '\n') | |
fs.writeFileSync( | |
outputLanguageDataDir + 'data.json', | |
`{ "en": ${JSON.stringify(defaultMessages, null, 2)} }` | |
) | |
console.log('Build : Language done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment