Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created October 7, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-ZubairAhmed/98ada20ce17956ef45228c6f1f0e8e1a to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/98ada20ce17956ef45228c6f1f0e8e1a to your computer and use it in GitHub Desktop.
Extracting all strings from react-intl to single json file
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