-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0aa7851
commit 7b1a8f2
Showing
5 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
const I18N_LOCATION = '../../auto_py_to_exe/web/js/i18n.js'; | ||
const README_LOCATION = '../../README.md'; | ||
|
||
const fs = require('fs'); | ||
const { supportedLanguages, translationMap } = require(I18N_LOCATION); | ||
const readmeContent = fs.readFileSync(README_LOCATION, 'utf8'); | ||
|
||
// Build up a report to return | ||
let report = 'Basic checks:'; | ||
|
||
// Validate if languages are sorted by name | ||
|
||
let supportedLanguagesSortedByNameError = null; | ||
for (let i = 1; i < supportedLanguages.length; i++) { | ||
if (supportedLanguages[i - 1].name.localeCompare(supportedLanguages[i].name) > 0) { | ||
supportedLanguagesSortedByNameError = `Sorting error: "${supportedLanguages[i - 1].name}" should be after "${ | ||
supportedLanguages[i].name | ||
}"`; | ||
} | ||
} | ||
|
||
if (supportedLanguagesSortedByNameError === null) { | ||
report += '\n- ✔️ i18n.js translationMap is sorted correctly'; | ||
} else { | ||
report += '\n- ❌ i18n.js translationMap is sorted correctly'; | ||
report += `\n\t- ${supportedLanguagesSortedByNameError}`; | ||
} | ||
|
||
// Validate the README translation table is sorted by name | ||
|
||
function extractTranslationTable(content) { | ||
const lines = content.split('\n'); | ||
|
||
let tableStart = false; | ||
let tableLines = []; | ||
for (let i = 0; i < lines.length; i++) { | ||
if (lines[i].trim() === '## Translations') { | ||
tableStart = true; | ||
i++; // Skip the next line (empty) | ||
continue; | ||
} | ||
|
||
if (tableStart) { | ||
if (lines[i].trim().startsWith('|')) { | ||
tableLines.push(lines[i].trim()); | ||
} else { | ||
break; // Stop when there are no more rows | ||
} | ||
} | ||
} | ||
|
||
// Skip the first two rows (header and ---) and then parse the data | ||
const tableRows = []; | ||
for (let i = 2; i < tableLines.length; i++) { | ||
const [_, language, translator, translated] = tableLines[i].split('|').map((x) => x.trim()); | ||
tableRows.push({ | ||
language, | ||
translator, | ||
translated, | ||
}); | ||
} | ||
|
||
return tableRows; | ||
} | ||
|
||
const readmeTranslationsTableRows = extractTranslationTable(readmeContent); | ||
|
||
let readmeTranslationsTableRowsSortedByLanguageError = null; | ||
for (let i = 1; i < readmeTranslationsTableRows.length; i++) { | ||
if (readmeTranslationsTableRows[i - 1].language.localeCompare(readmeTranslationsTableRows[i].language) > 0) { | ||
readmeTranslationsTableRowsSortedByLanguageError = `Sorting error: "${ | ||
readmeTranslationsTableRows[i - 1].language | ||
}" should be after "${readmeTranslationsTableRows[i].language}"`; | ||
} | ||
} | ||
|
||
if (readmeTranslationsTableRowsSortedByLanguageError === null) { | ||
report += '\n- ✔️ README.md translation table is sorted correctly'; | ||
} else { | ||
report += '\n- ❌ README.md translation table is sorted correctly'; | ||
report += `\n\t- ${readmeTranslationsTableRowsSortedByLanguageError}`; | ||
} | ||
|
||
// Identify missing translations in i18n.js | ||
|
||
const countMissingTranslations = (map, languages) => { | ||
const missingPaths = Object.fromEntries(languages.map((lang) => [lang, []])); | ||
|
||
const traverse = (node, path = []) => { | ||
if (typeof node !== 'object' || node === null) return; | ||
|
||
if (Object.keys(node).some((key) => languages.includes(key))) { | ||
languages.forEach((lang) => { | ||
if (!(lang in node)) { | ||
missingPaths[lang].push(path.join('.')); | ||
} | ||
}); | ||
} else { | ||
Object.entries(node).forEach(([key, value]) => traverse(value, [...path, key])); | ||
} | ||
}; | ||
|
||
traverse(map); | ||
return missingPaths; | ||
}; | ||
|
||
const allMissingTranslationPaths = countMissingTranslations( | ||
translationMap, | ||
supportedLanguages.map((l) => l.code) | ||
); | ||
|
||
// Match the i18n.js translations with the README translations table | ||
|
||
const translationMergeErrors = []; | ||
|
||
const mergedTranslations = supportedLanguages.map((l) => { | ||
const missingTranslationPaths = allMissingTranslationPaths[l.code]; | ||
const readmeRow = readmeTranslationsTableRows.find((r) => r.language === l.name); | ||
|
||
if (missingTranslationPaths === undefined) { | ||
translationMergeErrors.push(`Unable to find missing translation count for code "${l.code}"`); | ||
} | ||
if (readmeRow === undefined) { | ||
translationMergeErrors.push(`Unable to find README row for language "${l.name}"`); | ||
} | ||
|
||
return { | ||
...l, | ||
missingTranslationPaths, | ||
readmeRow, | ||
}; | ||
}); | ||
|
||
if (translationMergeErrors.length > 0) { | ||
report += '\n\nTranslation Errors:'; | ||
translationMergeErrors.forEach((e) => { | ||
report += `\n- ${e}`; | ||
}); | ||
} else { | ||
report += '\n\nTranslations:'; | ||
report += '\n\n| Name | Code | i18n.js Missing Count | Translator | Translated |'; | ||
report += '\n| ---- | ---- | --------------------- | ---------- | ---------- |'; | ||
mergedTranslations.forEach((t) => { | ||
const missingWithWarning = | ||
t.missingTranslationPaths.length === 0 | ||
? t.missingTranslationPaths.length | ||
: `${t.missingTranslationPaths.length} ⚠️`; | ||
report += `\n| ${t.name} | ${t.code} | ${missingWithWarning} | ${t.readmeRow.translator} | ${t.readmeRow.translated} |`; | ||
}); | ||
} | ||
|
||
report += '\n\nWarnings:'; | ||
mergedTranslations.forEach((t) => { | ||
t.missingTranslationPaths.forEach((path) => { | ||
report += `\n- ⚠️ ${t.name} (${t.code}) is missing a translation for ${path}`; | ||
}); | ||
}); | ||
|
||
console.log(report); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters