Created
January 30, 2018 11:46
-
-
Save liuwenzhuang/a01f85d9b25153776c5f3717cfae9253 to your computer and use it in GitHub Desktop.
为React-Intl提供json文件作为输入源,将其转换为在代码中defineMessages需要的对象
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
const fs = require('fs'); | |
const readline = require('readline'); | |
const writableStream = fs.createWriteStream('intl.js', { | |
flags: 'w', | |
autoClose: true | |
}); | |
const rl = readline.createInterface({ | |
input: fs.createReadStream('en.json'), | |
crlfDelay: Infinity | |
}); | |
rl.on('line', (line) => { | |
if(/[}{]/.test(line)) return; | |
const result = line.split(":"); | |
let left, right; | |
if(!result.length) return; | |
left = result[0]; | |
if(result.length > 2) { | |
right = result[1] + ":" + result[2]; | |
} | |
if(result.length === 2) { | |
right = result[1]; | |
} | |
let key; | |
const pointLoc = left.lastIndexOf("."); | |
if(pointLoc === -1) key = left; | |
key = left.substr(pointLoc + 1); | |
writableStream.write(`${key}:{\n`); | |
writableStream.write(` id: ${left},\n`); | |
writableStream.write(` defaultMessage: ${right}\n`); | |
writableStream.write(`},\n`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment