From 3e7d7205b8b841f619a77da6c2d28da88fef66a7 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 27 Mar 2018 20:09:15 -0700 Subject: i18n - cleanup verify locales util --- development/verify-locale-strings.js | 146 +++++++++++++++++++---------------- 1 file changed, 80 insertions(+), 66 deletions(-) (limited to 'development') diff --git a/development/verify-locale-strings.js b/development/verify-locale-strings.js index b8fe5a7dc..c0214d672 100644 --- a/development/verify-locale-strings.js +++ b/development/verify-locale-strings.js @@ -10,87 +10,101 @@ // //////////////////////////////////////////////////////////////////////////////// -var fs = require('fs') -var path = require('path') +const fs = require('fs') +const path = require('path') +const locales = 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 = locales.find(locale => locale.code === specifiedLocale) + verifyLocale({ locale }) +} else { + console.log('Verifying all locales:\n\n') + locales.forEach(locale => { + verifyLocale({ locale }) + 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({ locale }) { + const localeCode = locale.code + const localeName = locale.name + console.log(`Status of "${localeName}" (${localeCode})`) + + try { + const localeFilePath = path.join(process.cwd(), 'app', '_locales', localeCode, 'messages.json') + 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 ("${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') + 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) + } + 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') + + var counter = 0 + var foundErrorA = false + var notFound = []; + Object.keys(localeObj).forEach(function(key){ + if (!englishObj[key]) { + foundErrorA = true + notFound.push(key) + } + counter++ + }) -var counter = 0 -var foundErrorA = false -var notFound = []; -Object.keys(localeObj).forEach(function(key){ - if (!englishObj[key]) { - foundErrorA = true - notFound.push(key) + if (foundErrorA) { + console.log('\nMissing from english locale:') + notFound.forEach(function(key) { + console.log(` - [ ] ${key}`) + }) + } else { + // console.log(` all ${counter} strings declared in your locale ("${localeCode}") were found in the english one`) } - counter++ -}) -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('\n verifying whether your locale ("${localeCode}") contains all english strings') -console.log('\n\tverifying whether your locale contains all english strings') + var counter = 0 + var foundErrorB = false + var notFound = []; + Object.keys(englishObj).forEach(function(key){ + if (!localeObj[key]) { + foundErrorB = true + notFound.push(key) + } + counter++ + }) -var counter = 0 -var foundErrorB = false -var notFound = []; -Object.keys(englishObj).forEach(function(key){ - if (!localeObj[key]) { - foundErrorB = true - notFound.push(key) + if (foundErrorB) { + console.log(`\nMissing from "${localeCode}":`) + notFound.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 (!foundErrorA && !foundErrorB) { + console.log('You are good to go') + } } - -if (!foundErrorA && !foundErrorB) { - console.log('You are good to go') -} \ No newline at end of file -- cgit v1.2.3