scratch-www/test/localization/check_string_ids.js
jwzimmer bb5657587e Fix issue/gh 1313 by adding test to check for missing string ids (#1314)
* test for missing string ids

* Switched to multiple tests (1 per intl page) and changed comment style

* Add back in comment I accidentally . Add comment for the function.

* Change variable names, call function in-line
2017-05-24 10:11:07 -07:00

73 lines
2.5 KiB
JavaScript

/**
* Tests whether any page in www has any languages which are missing string IDs
* - checks every language against the list of english IDs for that page
* - test fails if the length of the list of languages missing any IDs is not 0
* - if the test fails, you can see which pages/ languages/ IDs are causing the failure:
* - Object.keys(pagesWithLanguagesMissingIds) gives you a list
* of the pages which had languages with missing IDs
* - pagesWithLanguagesMissingIds['pageName.intl.js'] gives you an object
* with languages as keys and the missing IDs as values
*/
var path = require('path');
var fs = require('fs');
var tap = require('tap');
/**
* To get the files (containing message IDs and localized strings for each page in www)
* from the intl directory
*/
var intlDirPath = path.resolve(__dirname, '../../intl/');
var intlFiles = fs.readdirSync(intlDirPath);
/**
* Tells tap whether the test should pass or fail for a given file.
* @param {string} fileName
* @param {Object} missingMessageId
* @param {Object} pagesMissingIds
*/
function noMissingStrings (fileName, missingMessageId, pagesMissingIds) {
if (Object.keys(missingMessageId).length == 0) {
tap.pass();
}
else {
tap.fail(fileName + ' is missing string IDs');
pagesMissingIds[fileName] = [];
pagesMissingIds[fileName].push(missingMessageId);
}
}
var pagesWithLanguagesMissingIds = {};
for (var i in intlFiles) {
var file = intlFiles[i];
var filePath = path.resolve(__dirname, '../../intl/' + file);
var pageMessagesString = fs.readFileSync(filePath,'utf8');
/**
* To make the string of the file of the page.intl.js back into useable objects
*/
var window = {};
var pageMessages = eval(pageMessagesString);
/**
* The goal is to compare the IDs for each language to the IDs for English,
* so we need the list of IDs for the given page in English
*/
var englishIdList = window._messages.en;
var messageIdNotInLanguage = {};
for (var languageKey in pageMessages) {
var currentLanguageObject = pageMessages[languageKey];
for (var messageId in englishIdList) {
if (! (messageId in currentLanguageObject)) {
if (typeof messageIdNotInLanguage[languageKey] == 'undefined') {
messageIdNotInLanguage[languageKey] = [];
}
messageIdNotInLanguage[languageKey].push(messageId);
}
}
}
noMissingStrings(file, messageIdNotInLanguage, pagesWithLanguagesMissingIds);
}