-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Usage: yarn run sync-strings ~/projects/projects/brave/ethereum-remote-client ~/projects/MetaMask/metamask-extension | ||
// Will write available strings from brave-dir to metmask-dir without adding/removing existing string identifiers in metmask-dir. | ||
|
||
const program = require('commander') | ||
const syncStrings = require('./sync-strings') | ||
|
||
program | ||
.command('sync-strings') | ||
.arguments('[sourceDir] [destDir]') | ||
.action(syncStrings) | ||
|
||
program | ||
.parse(process.argv) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
const assertDirExists = (dir) => { | ||
if (!fs.existsSync(dir)) { | ||
console.error('Source dir does not exist') | ||
process.exit(1) | ||
} | ||
} | ||
|
||
const getLangStringsMap = (dir) => { | ||
const langStringsMap = {} | ||
fs.readdirSync(dir) | ||
.filter(filterLangs) | ||
.forEach((lang) => { | ||
const langPath = path.join(dir, lang, 'messages.json') | ||
const langMap = JSON.parse(fs.readFileSync(langPath, 'utf8')) | ||
langStringsMap[lang] = {} | ||
Object.keys(langMap).forEach((key) => { | ||
langStringsMap[lang][key] = langMap[key].message | ||
}) | ||
}) | ||
return langStringsMap | ||
} | ||
|
||
const filterLangs = (lang) => | ||
!['index.json', 'en'].includes(lang) | ||
|
||
const syncLangStrings = (destDir, sourceLangStringsMap) => { | ||
fs.readdirSync(destDir) | ||
.filter(filterLangs) | ||
.forEach((lang) => { | ||
const langPath = path.join(destDir, lang, 'messages.json') | ||
// Create a lang map which is the entire dest dir content | ||
// this might get overwritten if the dest file already exists | ||
let langMap = Object.keys(sourceLangStringsMap[lang]).reduce((acc, cur) => { | ||
acc[cur] = { | ||
message: sourceLangStringsMap[lang][cur] | ||
} | ||
return acc | ||
}, {}) | ||
let newFile = true | ||
if (fs.existsSync(langPath)) { | ||
langMap = JSON.parse(fs.readFileSync(langPath, 'utf8')) | ||
newFile = false | ||
} | ||
Object.keys(langMap).forEach((key) => { | ||
if (sourceLangStringsMap[lang][key] || newFile) { | ||
langMap[key].message = | ||
formattingFixer(brandingFixer(sourceLangStringsMap[lang][key])) | ||
} | ||
}) | ||
const data = JSON.stringify(langMap, null, 2) | ||
console.log('writing lang path is: ', langPath) | ||
fs.writeFileSync(langPath, data + '\n', 'utf8') | ||
}) | ||
} | ||
|
||
const brandingFixer = (sourceString) => | ||
(sourceString || '') | ||
.replace(/Brave Software/g, 'Google') | ||
.replace(/Brave/g, 'Chrome') | ||
|
||
const formattingFixer = (sourceString) => | ||
(sourceString || '') | ||
.replace(/\n$/g, '') | ||
|
||
const syncDirs = (destDir, sourceLangs) => { | ||
sourceLangs.forEach((lang) => { | ||
const dir = path.join(destDir, lang) | ||
if (!fs.existsSync(dir)) { | ||
console.log('creating new dir: ', dir) | ||
fs.mkdirSync(dir) | ||
} | ||
}) | ||
} | ||
|
||
const syncStrings = (sourceDir, destDir) => { | ||
sourceDir = path.join(sourceDir, 'app', '_locales') | ||
destDir = path.join(destDir, 'app', '_locales') | ||
|
||
assertDirExists(sourceDir) | ||
assertDirExists(destDir) | ||
|
||
const sourceLangStringsMap = getLangStringsMap(sourceDir) | ||
const sourceLangs = Object.keys(sourceLangStringsMap) | ||
|
||
syncDirs(destDir, sourceLangs) | ||
syncLangStrings(destDir, sourceLangStringsMap) | ||
|
||
console.log(`Syncing from sourceDir: ${sourceDir} to destDir: ${destDir}`) | ||
|
||
// Read in all strings for all languages from sourceDir | ||
// Read in all strings for all languges from destDir | ||
} | ||
|
||
module.exports = syncStrings |