-
-
Save machour/0739cda4e5905f18c2061287a9b0bc40 to your computer and use it in GitHub Desktop.
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-disable import/no-extraneous-dependencies */ | |
/* eslint-disable import/no-dynamic-require */ | |
/* eslint-disable no-console */ | |
/* eslint-disable no-continue */ | |
const chalk = require('chalk'); | |
const glob = require('glob'); | |
const fs = require('fs'); | |
const babylon = require('babylon'); | |
const generate = require('babel-generator').default; | |
const traverse = require('babel-traverse').default; | |
const i18nConfig = require('./../package.json').i18n; | |
const languages = i18nConfig.languages; | |
const languagesContents = {}; | |
languages.forEach(lang => { | |
languagesContents[lang] = require(`../src/locale/languages/${lang}`); | |
}); | |
const originalLanguageContents = JSON.parse(JSON.stringify(languagesContents)); | |
const deepRead = (obj, arr) => { | |
if (arr.length === 0) return JSON.parse(JSON.stringify(obj)); | |
return deepRead(obj[arr[0]], arr.slice(1)); | |
}; | |
const english = { ...languagesContents.en }; | |
const migrate = config => { | |
const messages = {}; | |
config.sourcePath.forEach(source => { | |
const files = glob.sync(`${source}/**/*.js`, { debug: false }); | |
files.forEach(file => { | |
for (let i = 0; i < config.ignorePath.length; i++) { | |
if (file.indexOf(config.ignorePath[i]) !== -1) { | |
return; | |
} | |
} | |
const contents = fs.readFileSync(file).toString(); | |
const ast = babylon.parse(contents, { | |
sourceType: 'module', | |
plugins: ['jsx', 'classProperties', 'objectRestSpread', 'flow'], | |
}); | |
traverse(ast, { | |
enter: path => { | |
if (path.isImportSpecifier) { | |
if (path.node.imported && path.node.imported.name === 'translate') { | |
path.node.imported.name = 't'; | |
path.node.local.name = 't'; | |
} | |
} | |
if ( | |
path.isCallExpression(path.node) && | |
path.node.callee.name === 'translate' | |
) { | |
const identifier = path.node.arguments[0].value; | |
if (!identifier) { | |
console.log(path.node.arguments); | |
return; | |
} | |
let string = deepRead(english, identifier.split('.')); | |
if (identifier === 'common.abbreviations.thousand') { | |
string = '_thousandsAbbreviation'; | |
} | |
console.log(string); | |
Object.keys(languagesContents).forEach(lang => { | |
languagesContents[lang][string] = deepRead( | |
originalLanguageContents[lang], | |
identifier.split('.') | |
); | |
}); | |
path.node.callee.name = 't'; | |
path.node.arguments[0].value = string; | |
} | |
}, | |
}); | |
let counter = 1; | |
if (counter) { | |
let output = generate(ast, {}, contents); | |
fs.writeFileSync(file, output.code, 'utf8'); | |
// Write the new message file | |
} | |
}); | |
Object.keys(languagesContents).forEach(lang => { | |
fs.writeFileSync( | |
'src/locale/languages/' + lang + '.js', | |
`module.exports = ${JSON.stringify(languagesContents[lang], null, 2)};`, | |
'utf8' | |
); | |
}); | |
}); | |
return messages; | |
}; | |
migrate({ ...i18nConfig, translator: 'translate' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment