Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-gade committed Jul 24, 2024
1 parent fe40d81 commit ecf40e6
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 126 deletions.
36 changes: 16 additions & 20 deletions tests/translations/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,32 @@

module.exports = {
templates: {
include: [
"../../web/themes/new_weather_theme/templates/**/*.twig"
],
include: ["../../web/themes/new_weather_theme/templates/**/*.twig"],
exclude: [
'views-mini-pager.html.twig',
'pager.html.twig',
'menu-local-tasks.html.twig',
'breadcrumb.html.twig',
'maintenance-page.html.twig',
'field--comment.html.twig',
'filter-tips.html.twig',
'mark.html.twig',
'block--local-tasks-block.html.twig'
]
"views-mini-pager.html.twig",
"pager.html.twig",
"menu-local-tasks.html.twig",
"breadcrumb.html.twig",
"maintenance-page.html.twig",
"field--comment.html.twig",
"filter-tips.html.twig",
"mark.html.twig",
"block--local-tasks-block.html.twig",
],
},

php: {
include: [
"../../web/modules/weather_cms/**/*.php",
"../../web/modules/weather_cms/**/*.module",
"../../web/modules/weather_data/**/*.php",
"../../web/modules/weather_data/**/*.module"
"../../web/modules/weather_data/**/*.module",
],
exclude: []
exclude: [],
},

translations: {
include: [
"../../web/modules/weather_i18n/translations/*.po"
],
exclude: []
}
include: ["../../web/modules/weather_i18n/translations/*.po"],
exclude: [],
},
};
39 changes: 22 additions & 17 deletions tests/translations/gettextExtraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const path = require('path');
const { globSync } = require('glob');
const fs = require("fs");
const path = require("path");
const { globSync } = require("glob");

const MSG_ID_RX = /msgid\s+\"(.+)\"/m;
const MSG_STR_RX = /msgstr\s+\"(.*)\"/m;
Expand All @@ -12,44 +12,49 @@ const MSG_STR_RX = /msgstr\s+\"(.*)\"/m;
* Here a 'block' is any series of contiguous
* lines of text that are not just an empty string/newline.
*/
const parseGettextBlocks = str => {
const parseGettextBlocks = (str) => {
// We ignore the first block, which is just
// the gettext header information
return str.split("\n\n").slice(1);
};

const parseGettextSource = str => {
const parseGettextSource = (str) => {
const results = [];
const blocks = parseGettextBlocks(str);

blocks.forEach(block => {
const comments = block.split("\n").filter(line => {
blocks.forEach((block) => {
const comments = block.split("\n").filter((line) => {
return line.startsWith("#");
});
let msgidString, msgstrString, msgid, msgstr = null;
let msgidString,
msgstrString,
msgid,
msgstr = null;

const msgidMatch = block.match(MSG_ID_RX);
if(msgidMatch){
if (msgidMatch) {
msgidString = msgidMatch[0];
msgid = msgidMatch[1].replace(/\\n/g, "\n");
}

const msgstrMatch = block.match(MSG_STR_RX);
if(msgstrMatch){
if (msgstrMatch) {
msgstrString = msgstrMatch[0];
msgstr = msgstrMatch[1];
}

if(!msgstrMatch || !msgidMatch){
throw new Error(`Parse Error: Missing id or str pattern in block: ${block}`);
if (!msgstrMatch || !msgidMatch) {
throw new Error(
`Parse Error: Missing id or str pattern in block: ${block}`,
);
}

results.push({
comments,
msgid,
msgstr,
msgidString,
msgstrString
msgstrString,
});
});

Expand All @@ -61,21 +66,21 @@ const parseGettextSource = str => {
* respond with a dictionary mapping filenames
* to match information for the gettext values
*/
const getTranslationMatchInfo = sourcePaths => {
const getTranslationMatchInfo = (sourcePaths) => {
const lookup = {};

sourcePaths.forEach(filePath => {
sourcePaths.forEach((filePath) => {
const languageCode = path.basename(filePath).split(".")[0];
const langLookup = {};
const source = fs.readFileSync(filePath).toString();
const parsed = parseGettextSource(source);
parsed.forEach(entry => langLookup[entry.msgid] = entry);
parsed.forEach((entry) => (langLookup[entry.msgid] = entry));
lookup[languageCode] = langLookup;
});

return lookup;
};

module.exports = {
getTranslationMatchInfo
getTranslationMatchInfo,
};
79 changes: 43 additions & 36 deletions tests/translations/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,46 @@ const { getFileMatchInfo } = require("./translationExtraction");
const { getTranslationMatchInfo } = require("./gettextExtraction");
const config = require("./config.js");


/**
* Get all of the template and php paths as flat arrays
*/
const templatePaths = config.templates.include.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
},[]).filter(filePath => {
const fileName = path.basename(filePath);
return !config.templates.exclude.includes(fileName);
});
const phpPaths = config.php.include.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
}, []).filter(filePath => {
const fileName = path.basename(filePath);
return !config.php.exclude.includes(fileName);
});
const translationPaths = config.translations.include.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
}, []).filter(filePath => {
const fileName = path.basename(filePath);
return !config.translations.exclude.includes(fileName);
});
const templatePaths = config.templates.include
.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
}, [])
.filter((filePath) => {
const fileName = path.basename(filePath);
return !config.templates.exclude.includes(fileName);
});
const phpPaths = config.php.include
.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
}, [])
.filter((filePath) => {
const fileName = path.basename(filePath);
return !config.php.exclude.includes(fileName);
});
const translationPaths = config.translations.include
.reduce((prev, current) => {
const relativeGlob = path.resolve(__dirname, current);
const filePaths = globSync(relativeGlob);
return prev.concat(filePaths);
}, [])
.filter((filePath) => {
const fileName = path.basename(filePath);
return !config.translations.exclude.includes(fileName);
});

const templateLookup = getFileMatchInfo(templatePaths, phpPaths);
const translationLookup = getTranslationMatchInfo(translationPaths);
const languages = Object.keys(translationLookup);
let errorsSummary = [];

languages.forEach(langCode => {
languages.forEach((langCode) => {
console.log(`Checking translation integrity for: ${langCode}`);
// First get any translations defined in templates
// that are missing from the translation file for this
Expand All @@ -48,25 +53,25 @@ languages.forEach(langCode => {
const templateTerms = Object.keys(templateLookup);

let fileNames = new Set();
templateTerms.forEach(key => {
templateLookup[key].forEach(phrase => {
templateTerms.forEach((key) => {
templateLookup[key].forEach((phrase) => {
fileNames.add(phrase.filename);
});
});

console.log("Checking for missing translations");
const missing = templateTerms.filter(key => {
const missing = templateTerms.filter((key) => {
return !translationTerms.includes(key);
});

if(missing.length){
if (missing.length) {
const errString = `Missing [${missing.length}] translations in the ${langCode} translations file`;
errorsSummary.push(errString);
console.error(errString + ":");
missing.forEach(key => {
missing.forEach((key) => {
const entryList = templateLookup[key];
if(entryList){
const fileLocations = entryList.map(entry => {
if (entryList) {
const fileLocations = entryList.map((entry) => {
return `${entry.filename}:${entry.lineNumber}`;
});
const serialized = JSON.stringify(entryList, null, 2);
Expand All @@ -77,12 +82,14 @@ languages.forEach(langCode => {
}

console.log("Checking for stale translations");
const stale = translationTerms.filter(key => {
const stale = translationTerms.filter((key) => {
return !templateTerms.includes(key);
});

if(stale.length){
console.warn(`Found ${stale.length} potentially stale translations in the ${langCode} file`);
if (stale.length) {
console.warn(
`Found ${stale.length} potentially stale translations in the ${langCode} file`,
);
console.log(stale);
}
});
14 changes: 11 additions & 3 deletions tests/translations/tests/twig-t-filter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ describe("Twig translation regex tests", () => {
describe("Basic cases", () => {
let source;
before(() => {
source = fs.readFileSync(path.resolve(__dirname, "fixtures", "t.filter.basic.twig")).toString();
source = fs
.readFileSync(
path.resolve(__dirname, "fixtures", "t.filter.basic.twig"),
)
.toString();
});

it("Matches the correct number of translations", () => {
Expand Down Expand Up @@ -43,7 +47,9 @@ describe("Twig translation regex tests", () => {
describe("Embedded HTML cases", () => {
let source;
before(() => {
source = fs.readFileSync(path.resolve(__dirname, "fixtures", "t.filter.html.twig")).toString();
source = fs
.readFileSync(path.resolve(__dirname, "fixtures", "t.filter.html.twig"))
.toString();
});

it("Has the correct number of matches", () => {
Expand Down Expand Up @@ -74,7 +80,9 @@ describe("Twig translation regex tests", () => {
describe("Variable setting cases", () => {
let source;
before(() => {
source = fs.readFileSync(path.resolve(__dirname, "fixtures", "t.variable.twig")).toString();
source = fs
.readFileSync(path.resolve(__dirname, "fixtures", "t.variable.twig"))
.toString();
});

it("Has the correct number of matches", () => {
Expand Down
Loading

0 comments on commit ecf40e6

Please sign in to comment.