Skip to content

Commit

Permalink
Translations: Tests: Add new IncorrectNamespaceUsageTest
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeySafronov committed Dec 27, 2024
1 parent 4bd1527 commit c2c3c50
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions common/tests/test/locales.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,4 +813,53 @@ describe("Locales Tests", () => {

expect(exists, message).toBe(false);
});

test("IncorrectNamespaceUsageTest: Verify that translation keys are used with their correct namespace", () => {
let message = "The following keys are using incorrect namespaces:\r\n\r\n";
let incorrectUsages = [];

// Create a map of all available keys in each namespace
const namespaceKeys = {};
translationFiles.forEach(file => {
const namespace = path.basename(file.fileName, ".json");
namespaceKeys[namespace] = new Set(file.translations.map(t => t.key));
});

// Check each JavaScript file for translation key usage
javascriptFiles.forEach(jsFile => {
jsFile.translationKeys.forEach(key => {
const [namespace, translationKey] = key.split(":");

// Skip if the key doesn't follow namespace:key format
if (!translationKey) return;

// Check if the key exists in the specified namespace
if (namespaceKeys[namespace] && !namespaceKeys[namespace].has(translationKey)) {
// Check if the key exists in other namespaces
const foundInNamespaces = Object.entries(namespaceKeys)
.filter(([ns, keys]) => ns !== namespace && keys.has(translationKey))
.map(([ns]) => ns);

if (foundInNamespaces.length > 0) {
incorrectUsages.push({
file: jsFile.path,
key: key,
correctNamespaces: foundInNamespaces
});
}
}
});
});

if (incorrectUsages.length > 0) {
let i = 1;
message += incorrectUsages
.map(usage => `${i++}. File: ${usage.file}\n Key: ${usage.key}\n Correct namespace(s): ${usage.correctNamespaces.join(", ")}\n`)
.join("\n");

console.log(message);
}

expect(incorrectUsages.length, message).toBe(0);
});
});

0 comments on commit c2c3c50

Please sign in to comment.