1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/development/verify-locale-strings.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-07-03 00:49:33 +02:00
// //////////////////////////////////////////////////////////////////////////////
2018-03-19 17:51:51 +01:00
//
// Locale verification script
//
// usage:
//
// node app/scripts/verify-locale-strings.js <locale>
//
// will check the given locale against the strings in english
//
2018-07-03 00:49:33 +02:00
// //////////////////////////////////////////////////////////////////////////////
2018-03-19 17:51:51 +01:00
2018-03-28 05:09:15 +02:00
const fs = require('fs')
const path = require('path')
2018-03-28 05:29:29 +02:00
const localeIndex = require('../app/_locales/index.json')
2018-03-19 17:51:51 +01:00
console.log('Locale Verification')
2018-03-28 05:09:15 +02:00
const specifiedLocale = process.argv[2]
if (specifiedLocale) {
console.log(`Verifying selected locale "${specifiedLocale}":\n\n`)
2018-03-28 05:29:29 +02:00
const locale = localeIndex.find(localeMeta => localeMeta.code === specifiedLocale)
verifyLocale(locale)
2018-03-28 05:09:15 +02:00
} else {
console.log('Verifying all locales:\n\n')
2018-03-28 05:29:29 +02:00
localeIndex.forEach(localeMeta => {
verifyLocale({ localeMeta })
2018-03-28 05:09:15 +02:00
console.log('\n')
})
2018-03-19 17:51:51 +01:00
}
function verifyLocale ({ localeMeta }) {
2018-03-28 05:29:29 +02:00
const localeCode = localeMeta.code
const localeName = localeMeta.name
2018-07-03 00:58:31 +02:00
let targetLocale, englishLocale
2018-03-28 05:09:15 +02:00
try {
const localeFilePath = path.join(process.cwd(), 'app', '_locales', localeCode, 'messages.json')
2018-07-03 00:49:33 +02:00
targetLocale = JSON.parse(fs.readFileSync(localeFilePath, 'utf8'))
2018-03-28 05:09:15 +02:00
} catch (e) {
2018-07-03 00:58:31 +02:00
if (e.code === 'ENOENT') {
2018-03-28 05:09:15 +02:00
console.log('Locale file not found')
} else {
console.log(`Error opening your locale ("${localeCode}") file: `, e)
}
process.exit(1)
2018-03-19 17:51:51 +01:00
}
2018-03-28 05:09:15 +02:00
try {
const englishFilePath = path.join(process.cwd(), 'app', '_locales', 'en', 'messages.json')
2018-07-03 00:49:33 +02:00
englishLocale = JSON.parse(fs.readFileSync(englishFilePath, 'utf8'))
2018-03-28 05:09:15 +02:00
} catch (e) {
2018-07-03 00:58:31 +02:00
if (e.code === 'ENOENT') {
2018-03-28 05:09:15 +02:00
console.log('English File not found')
} else {
console.log('Error opening english locale file: ', e)
}
process.exit(1)
2018-03-19 17:51:51 +01:00
}
2018-03-28 05:09:15 +02:00
// console.log(' verifying whether all your locale ("${localeCode}") strings are contained in the english one')
2018-03-28 05:29:29 +02:00
const extraItems = compareLocalesForMissingItems({ base: targetLocale, subject: englishLocale })
// console.log('\n verifying whether your locale ("${localeCode}") contains all english strings')
const missingItems = compareLocalesForMissingItems({ base: englishLocale, subject: targetLocale })
2018-03-28 05:09:15 +02:00
2018-03-28 05:29:29 +02:00
const englishEntryCount = Object.keys(englishLocale).length
const coveragePercent = 100 * (englishEntryCount - missingItems.length) / englishEntryCount
2018-03-19 17:51:51 +01:00
2018-03-28 05:29:29 +02:00
console.log(`Status of **${localeName} (${localeCode})** ${coveragePercent.toFixed(2)}% coverage:`)
if (extraItems.length) {
2018-03-28 05:09:15 +02:00
console.log('\nMissing from english locale:')
2018-07-03 00:49:33 +02:00
extraItems.forEach(function (key) {
2018-03-28 05:09:15 +02:00
console.log(` - [ ] ${key}`)
})
} else {
// console.log(` all ${counter} strings declared in your locale ("${localeCode}") were found in the english one`)
2018-03-19 17:51:51 +01:00
}
2018-03-28 05:29:29 +02:00
if (missingItems.length) {
console.log(`\nMissing:`)
2018-07-03 00:49:33 +02:00
missingItems.forEach(function (key) {
2018-03-28 05:09:15 +02:00
console.log(` - [ ] ${key}`)
})
} else {
// console.log(` all ${counter} english strings were found in your locale ("${localeCode}")!`)
2018-03-19 17:51:51 +01:00
}
2018-03-28 05:29:29 +02:00
if (!extraItems.length && !missingItems.length) {
console.log('Full coverage : )')
2018-03-28 05:09:15 +02:00
}
2018-03-19 17:51:51 +01:00
}
2018-03-28 05:29:29 +02:00
2018-07-03 00:49:33 +02:00
function compareLocalesForMissingItems ({ base, subject }) {
2018-03-28 05:29:29 +02:00
return Object.keys(base).filter((key) => !subject[key])
}