scratch-www/test/localization-legacy/check_string_ids.js

73 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-17 14:56:48 -04:00
/**
* 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
*/
const path = require('path');
const fs = require('fs');
const tap = require('tap');
2019-07-17 14:56:48 -04:00
/**
* To get the files (containing message IDs and localized strings for each page in www)
* from the intl directory
*/
const intlDirPath = path.resolve(__dirname, '../../intl/');
const intlFiles = fs.readdirSync(intlDirPath);
2019-07-17 14:56:48 -04:00
/*
* Tells tap whether the test should pass or fail for a given file.
* @param {string} fileName
* @param {Object} missingMessageId
* @param {Object} pagesMissingIds
*/
const noMissingStrings = (fileName, missingMessageId, pagesMissingIds) => {
if (Object.keys(missingMessageId).length === 0) {
tap.pass();
} else {
tap.fail(`${fileName} is missing string IDs`);
2019-07-17 14:56:48 -04:00
pagesMissingIds[fileName] = [];
pagesMissingIds[fileName].push(missingMessageId);
}
};
const pagesWithLanguagesMissingIds = {};
2019-07-17 14:56:48 -04:00
for (const i in intlFiles) {
const file = intlFiles[i];
const filePath = path.resolve(__dirname, `../../intl/${file}`);
const pageMessagesString = fs.readFileSync(filePath, 'utf8');
2019-07-17 14:56:48 -04:00
/**
* To make the string of the file of the page.intl.js back into useable objects
*/
const window = {};
const pageMessages = eval(pageMessagesString); // eslint-disable-line no-eval
2019-07-17 14:56:48 -04:00
/**
* 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
*/
const englishIdList = window._messages.en;
2019-07-17 14:56:48 -04:00
const messageIdNotInLanguage = {};
2019-07-17 14:56:48 -04:00
for (const languageKey in pageMessages) {
const currentLanguageObject = pageMessages[languageKey];
for (const messageId in englishIdList) {
2019-07-17 14:56:48 -04:00
if (!(messageId in currentLanguageObject)) {
if (typeof messageIdNotInLanguage[languageKey] === 'undefined') {
messageIdNotInLanguage[languageKey] = [];
}
messageIdNotInLanguage[languageKey].push(messageId);
}
}
}
noMissingStrings(file, messageIdNotInLanguage, pagesWithLanguagesMissingIds);
}