Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes & Improvements to i18n linting #16

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix linting nested translations
t3chguy committed Feb 28, 2024
commit 0738f0760cc38b446a5b9bf083d29acc6a07a5b7
29 changes: 26 additions & 3 deletions scripts/lint-i18n.ts
Original file line number Diff line number Diff line change
@@ -23,26 +23,49 @@ limitations under the License.
*/

import { getTranslations, isPluralisedTranslation } from "./common";
import { KEY_SEPARATOR, Translation, Translations } from "../src";

const input = getTranslations();

const filtered = Object.keys(input).filter(key => {
const value = input[key];
function lintTranslation(keys: string[], value: Translation): boolean {
const key = keys[keys.length - 1];
const printableKey = keys.join(KEY_SEPARATOR);

// Check for invalid characters in the translation key
if (!!key.replace(/[a-z0-9_]+/g, "")) {
console.log(`"${key}": key contains invalid characters`);
console.log(`"${printableKey}": key contains invalid characters`);
return true;
}

// Check that the translated string does not match the key.
if (key === input[key] || (isPluralisedTranslation(value) && (key === value.other || key === value.one))) {
console.log(`"${key}": key matches value`);
console.log(`"${printableKey}": key matches value`);
return true;
}

return false;
});
}

function traverseTranslations(translations: Translations, keys: string[] = []): string[] {
const filtered: string[] = [];
Object.keys(translations).forEach(key => {
const value = translations[key];

if (typeof value === "object" && !isPluralisedTranslation(value)) {
filtered.push(...traverseTranslations(value, [...keys, key]));
return;
}

if (lintTranslation([...keys, key], value)) {
filtered.push(key);
}
});
return filtered;
}

const filtered = traverseTranslations(input);

if (filtered.length > 0) {
console.log(`${filtered.length} invalid translation keys`);