Created
March 15, 2017 22:04
-
-
Save jazmon/9048da68ac216c2ef31e90ba99ac3487 to your computer and use it in GitHub Desktop.
React Native i18n -- createTranslation.js
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
/* eslint-env node, es6 */ | |
// This is a quick 'n' dirty script to create the translation.json bundle. | |
// It crawls the locales folder and each locales subfolder and appends all namespaces to the bundle. | |
const fs = require('fs'); | |
const path = require('path'); | |
const localesPath = path.resolve(__dirname, '..', 'locales'); | |
const outputPath = path.resolve(__dirname, '..', 'translation.json'); | |
const json = {}; | |
const list = fs.readdirSync(localesPath, 'utf8'); | |
list.forEach((directory) => { | |
const tempFile = path.join(localesPath, directory); | |
const stat = fs.statSync(tempFile); | |
if (stat && stat.isDirectory()) { | |
const fileList = fs.readdirSync(tempFile, 'utf8'); | |
fileList.forEach((file) => { | |
const moduleName = file.split('.json')[0]; | |
if (!json[directory]) json[directory] = {}; | |
const data = fs.readFileSync(path.join(tempFile, file), 'utf8'); | |
json[directory][moduleName] = JSON.parse(data); | |
}); | |
} | |
}); | |
fs.writeFileSync(outputPath, JSON.stringify(json, null, 2), 'utf8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment