Skip to content

Instantly share code, notes, and snippets.

@chochinlu
Created December 2, 2020 08:44
Show Gist options
  • Save chochinlu/fccf1c83c88e975774b6968fa7bee376 to your computer and use it in GitHub Desktop.
Save chochinlu/fccf1c83c88e975774b6968fa7bee376 to your computer and use it in GitHub Desktop.
merge i18n files to one csv file
const fs = require('fs')
const path = require('path')
const I18N_DIR = path.join('src', 'i18n', 'resources')
const TW_DIR = path.join(I18N_DIR, 'zh-TW')
const EN_DIR = path.join(I18N_DIR, 'en')
const DELIMITER = '^' // 分隔符
const TARGET = 'target.csv'
function getJsonFileList(dirName) {
const list = fs.readdirSync(dirName)
return list.filter((name) => name !== 'index.ts')
}
function getParsedData(dirName, fileName) {
if (!fileExist(dirName, fileName)) {
return null
}
const rawData = fs.readFileSync(path.resolve(dirName, fileName))
return JSON.parse(rawData)
}
function fileExist(dirName, fileName) {
return fs.existsSync(path.resolve(dirName, fileName))
}
// --- Main Flow ---
for (const f of getJsonFileList(TW_DIR)) {
const parsedTwData = getParsedData(TW_DIR, f)
const parsedEnData = getParsedData(EN_DIR, f)
for (const key of Object.keys(parsedTwData)) {
let row = [
key,
parsedTwData[key],
parsedEnData ? parsedEnData[key] : '',
].join(DELIMITER)
row = row + '\n'
fs.appendFileSync(TARGET, row)
}
}
console.log('Done! Output to ', TARGET)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment