|
import { describe, expect, it } from 'vitest';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
describe('translation.json', () => {
|
|
it('should not have duplicate translation keys', () => {
|
|
|
|
const translationPath = path.join(__dirname, '../../src/i18n/translation.json');
|
|
const translationContent = fs.readFileSync(translationPath, 'utf-8');
|
|
|
|
|
|
const keyRegex = /"([^"]+)": {/g;
|
|
const matches = translationContent.matchAll(keyRegex);
|
|
const keyOccurrences = new Map<string, number>();
|
|
const duplicateKeys: string[] = [];
|
|
|
|
for (const match of matches) {
|
|
const key = match[1];
|
|
const count = (keyOccurrences.get(key) || 0) + 1;
|
|
keyOccurrences.set(key, count);
|
|
if (count > 1) {
|
|
duplicateKeys.push(key);
|
|
}
|
|
}
|
|
|
|
|
|
const uniqueDuplicates = [...new Set(duplicateKeys)];
|
|
|
|
|
|
if (uniqueDuplicates.length > 0) {
|
|
const errorMessage = `Found duplicate translation keys:\n${uniqueDuplicates
|
|
.map((key) => ` - "${key}" appears ${keyOccurrences.get(key)} times`)
|
|
.join('\n')}`;
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
|
|
expect(uniqueDuplicates).toHaveLength(0);
|
|
});
|
|
|
|
it('should have consistent translations for each key', () => {
|
|
|
|
const translationPath = path.join(__dirname, '../../src/i18n/translation.json');
|
|
const translationContent = fs.readFileSync(translationPath, 'utf-8');
|
|
const translations = JSON.parse(translationContent);
|
|
|
|
|
|
const englishTranslations = new Map<string, string>();
|
|
const inconsistentKeys: string[] = [];
|
|
|
|
|
|
Object.entries(translations).forEach(([key, value]: [string, any]) => {
|
|
if (typeof value === 'object' && value.en !== undefined) {
|
|
const currentEn = value.en.toLowerCase();
|
|
const existingEn = englishTranslations.get(key)?.toLowerCase();
|
|
|
|
if (existingEn !== undefined && existingEn !== currentEn) {
|
|
inconsistentKeys.push(key);
|
|
} else {
|
|
englishTranslations.set(key, value.en);
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
if (inconsistentKeys.length > 0) {
|
|
const errorMessage = `Found inconsistent translations for keys:\n${inconsistentKeys
|
|
.map((key) => ` - "${key}" has multiple different English translations`)
|
|
.join('\n')}`;
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
|
|
expect(inconsistentKeys).toHaveLength(0);
|
|
});
|
|
});
|
|
|