Files
mygit/ghost/i18n/test/utils.js
T
DuckQ1u 93d1b7c3d3
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
first commit
2026-04-22 19:51:20 +07:00

47 lines
1.0 KiB
JavaScript

function extractVariables(str) {
if (!str) {
return new Set();
}
const regex = /\{([^}]+)\}/g;
const variables = new Set();
let match;
while ((match = regex.exec(str)) !== null) {
variables.add(match[1]);
}
return variables;
}
function checkTranslationPair(key, value) {
let result = [];
// Skip checking if value is an empty string
if (value === '') {
return result;
}
if (typeof key !== 'string') {
return result;
}
const keyVars = extractVariables(key);
const valueVars = extractVariables(value);
// Check for variables in key but not in value
for (const keyVar of keyVars) {
if (!valueVars.has(keyVar)) {
result.push('missingVariable');
}
}
// Check for variables in value but not in key
for (const valueVar of valueVars) {
if (!keyVars.has(valueVar)) {
result.push('addedVariable');
}
}
return result;
}
module.exports = {
checkTranslationPair
};