File size: 2,937 Bytes
246d201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { describe, expect, it } from 'vitest';
import fs from 'fs';
import path from 'path';

describe('translation.json', () => {
  it('should not have duplicate translation keys', () => {
    // Read the translation.json file
    const translationPath = path.join(__dirname, '../../src/i18n/translation.json');
    const translationContent = fs.readFileSync(translationPath, 'utf-8');

    // First, let's check for exact string matches of key definitions
    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);
      }
    }

    // Remove duplicates from duplicateKeys array
    const uniqueDuplicates = [...new Set(duplicateKeys)];

    // If there are duplicates, create a helpful error message
    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 no duplicates (this will pass if we reach here)
    expect(uniqueDuplicates).toHaveLength(0);
  });

  it('should have consistent translations for each key', () => {
    // Read the translation.json file
    const translationPath = path.join(__dirname, '../../src/i18n/translation.json');
    const translationContent = fs.readFileSync(translationPath, 'utf-8');
    const translations = JSON.parse(translationContent);

    // Create a map to store English translations for each key
    const englishTranslations = new Map<string, string>();
    const inconsistentKeys: string[] = [];

    // Check each key's English translation
    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 there are inconsistencies, create a helpful error message
    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 no inconsistencies
    expect(inconsistentKeys).toHaveLength(0);
  });
});