aboutsummaryrefslogtreecommitdiffstats
path: root/development/verify-locale-strings.js
diff options
context:
space:
mode:
Diffstat (limited to 'development/verify-locale-strings.js')
-rw-r--r--development/verify-locale-strings.js133
1 files changed, 67 insertions, 66 deletions
diff --git a/development/verify-locale-strings.js b/development/verify-locale-strings.js
index b8fe5a7dc..8dc0a30f1 100644
--- a/development/verify-locale-strings.js
+++ b/development/verify-locale-strings.js
@@ -10,87 +10,88 @@
//
////////////////////////////////////////////////////////////////////////////////
-var fs = require('fs')
-var path = require('path')
+const fs = require('fs')
+const path = require('path')
+const localeIndex = require('../app/_locales/index.json')
console.log('Locale Verification')
-var locale = process.argv[2]
-if (!locale || locale == '') {
- console.log('Must enter a locale as argument. exitting')
- process.exit(1)
+const specifiedLocale = process.argv[2]
+if (specifiedLocale) {
+ console.log(`Verifying selected locale "${specifiedLocale}":\n\n`)
+ const locale = localeIndex.find(localeMeta => localeMeta.code === specifiedLocale)
+ verifyLocale({ localeMeta })
+} else {
+ console.log('Verifying all locales:\n\n')
+ localeIndex.forEach(localeMeta => {
+ verifyLocale({ localeMeta })
+ console.log('\n')
+ })
}
-console.log("verifying for locale " + locale)
-localeFilePath = path.join(process.cwd(), 'app', '_locales', locale, 'messages.json')
-try {
- localeObj = JSON.parse(fs.readFileSync(localeFilePath, 'utf8'));
-} catch (e) {
- if(e.code == 'ENOENT') {
- console.log('Locale file not found')
- } else {
- console.log('Error opening your locale file: ', e)
+
+function verifyLocale({ localeMeta }) {
+ const localeCode = localeMeta.code
+ const localeName = localeMeta.name
+
+ try {
+ const localeFilePath = path.join(process.cwd(), 'app', '_locales', localeCode, 'messages.json')
+ targetLocale = JSON.parse(fs.readFileSync(localeFilePath, 'utf8'));
+ } catch (e) {
+ if (e.code == 'ENOENT') {
+ console.log('Locale file not found')
+ } else {
+ console.log(`Error opening your locale ("${localeCode}") file: `, e)
+ }
+ process.exit(1)
}
- process.exit(1)
-}
-englishFilePath = path.join(process.cwd(), 'app', '_locales', 'en', 'messages.json')
-try {
- englishObj = JSON.parse(fs.readFileSync(englishFilePath, 'utf8'));
-} catch (e) {
- if(e.code == 'ENOENT') {
- console.log("English File not found")
- } else {
- console.log("Error opening english locale file: ", e)
+ try {
+ const englishFilePath = path.join(process.cwd(), 'app', '_locales', 'en', 'messages.json')
+ englishLocale = JSON.parse(fs.readFileSync(englishFilePath, 'utf8'));
+ } catch (e) {
+ if(e.code == 'ENOENT') {
+ console.log('English File not found')
+ } else {
+ console.log('Error opening english locale file: ', e)
+ }
+ process.exit(1)
}
- process.exit(1)
-}
-console.log('\tverifying whether all your locale strings are contained in the english one')
+ // console.log(' verifying whether all your locale ("${localeCode}") strings are contained in the english one')
+ 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 })
-var counter = 0
-var foundErrorA = false
-var notFound = [];
-Object.keys(localeObj).forEach(function(key){
- if (!englishObj[key]) {
- foundErrorA = true
- notFound.push(key)
- }
- counter++
-})
+ const englishEntryCount = Object.keys(englishLocale).length
+ const coveragePercent = 100 * (englishEntryCount - missingItems.length) / englishEntryCount
-if (foundErrorA) {
- console.log('\nThe following string(s) is(are) not found in the english locale:')
- notFound.forEach(function(key) {
- console.log(key)
- })
-} else {
- console.log('\tall ' + counter +' strings declared in your locale were found in the english one')
-}
+ console.log(`Status of **${localeName} (${localeCode})** ${coveragePercent.toFixed(2)}% coverage:`)
-console.log('\n\tverifying whether your locale contains all english strings')
+ if (extraItems.length) {
+ console.log('\nMissing from english locale:')
+ extraItems.forEach(function(key) {
+ console.log(` - [ ] ${key}`)
+ })
+ } else {
+ // console.log(` all ${counter} strings declared in your locale ("${localeCode}") were found in the english one`)
+ }
-var counter = 0
-var foundErrorB = false
-var notFound = [];
-Object.keys(englishObj).forEach(function(key){
- if (!localeObj[key]) {
- foundErrorB = true
- notFound.push(key)
+ if (missingItems.length) {
+ console.log(`\nMissing:`)
+ missingItems.forEach(function(key) {
+ console.log(` - [ ] ${key}`)
+ })
+ } else {
+ // console.log(` all ${counter} english strings were found in your locale ("${localeCode}")!`)
}
- counter++
-})
-if (foundErrorB) {
- console.log('\nThe following string(s) is(are) not found in the your locale:')
- notFound.forEach(function(key) {
- console.log(key)
- })
-} else {
- console.log('\tall ' + counter +' english strings were found in your locale!')
+ if (!extraItems.length && !missingItems.length) {
+ console.log('Full coverage : )')
+ }
}
-if (!foundErrorA && !foundErrorB) {
- console.log('You are good to go')
-} \ No newline at end of file
+function compareLocalesForMissingItems({ base, subject }) {
+ return Object.keys(base).filter((key) => !subject[key])
+}