Skip to content

Commit dd50300

Browse files
Merge pull request #2306 from sagarkumar-webkul/script/lang
Add language file validation test to ensure key consistency with en ( English) app.php
2 parents 6c7dbca + 48e3737 commit dd50300

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
import { test, expect } from '@playwright/test';
5+
6+
// Resolve file paths relative to this test file
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
9+
10+
// Absolute path to /lang directory
11+
const LANG_DIR = path.resolve(
12+
__dirname,
13+
'../../../../src/Resources/lang'
14+
);
15+
16+
const BASE_LANG = 'en';
17+
18+
// Helper to extract just the keys from app.php
19+
function getNormalizedKeys(filePath: string): string[] {
20+
const raw = fs.readFileSync(filePath, 'utf-8');
21+
22+
const clean = raw
23+
.replace(/<\?php\s*/g, '')
24+
.replace(/return\s*\[/, '')
25+
.replace(/\];/, '')
26+
.trim();
27+
28+
return clean
29+
.split('\n')
30+
.map(line => line.trim())
31+
.filter(line => line.includes('=>') && !line.startsWith('//'))
32+
.map(line => {
33+
// Extract the key before =>
34+
const match = line.match(/^['"](.+?)['"]\s*=>/);
35+
return match ? match[1] : null;
36+
})
37+
.filter(Boolean) as string[];
38+
}
39+
test('All language files must match number of keys and key names with English app.php', () => {
40+
const baseFile = path.join(LANG_DIR, BASE_LANG, 'app.php');
41+
const baseKeys = getNormalizedKeys(baseFile);
42+
// All locales except the base one
43+
const locales = fs
44+
.readdirSync(LANG_DIR)
45+
.filter(locale => locale !== BASE_LANG && fs.existsSync(path.join(LANG_DIR, locale, 'app.php')));
46+
47+
// Array to collect any locales that have issues
48+
let failedLocales: { locale: string; missingKeys: string[]; extraKeys: string[] }[] = [];
49+
50+
for (const locale of locales) {
51+
const filePath = path.join(LANG_DIR, locale, 'app.php');
52+
const keys = getNormalizedKeys(filePath);
53+
54+
const missingKeys = baseKeys.filter(key => !keys.includes(key));
55+
const extraKeys = keys.filter(key => !baseKeys.includes(key));
56+
57+
if (missingKeys.length > 0 || extraKeys.length > 0) {
58+
failedLocales.push({ locale, missingKeys, extraKeys });
59+
}
60+
}
61+
62+
if (failedLocales.length > 0) {
63+
for (const { locale, missingKeys, extraKeys } of failedLocales) {
64+
console.error(` ${locale}/app.php has issues:`);
65+
66+
if (missingKeys.length) {
67+
console.error(` Missing keys (${missingKeys.length}):`);
68+
for (const key of missingKeys) console.error(` - ${key}`);
69+
}
70+
71+
if (extraKeys.length) {
72+
console.error(` Extra keys (${extraKeys.length}):`);
73+
for (const key of extraKeys) console.error(` + ${key}`);
74+
}
75+
}
76+
77+
// Fail the test
78+
expect(failedLocales).toEqual([]);
79+
} else {
80+
console.log('All language files have matching keys and counts with en/app.php');
81+
}
82+
});

0 commit comments

Comments
 (0)