Skip to content
Merged
Changes from all commits
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
82 changes: 82 additions & 0 deletions packages/Webkul/Admin/tests/e2e-pw/tests/lang/lang.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { test, expect } from '@playwright/test';

// Resolve file paths relative to this test file
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Absolute path to /lang directory
const LANG_DIR = path.resolve(
__dirname,
'../../../../src/Resources/lang'
);

const BASE_LANG = 'en';

// Helper to extract just the keys from app.php
function getNormalizedKeys(filePath: string): string[] {
const raw = fs.readFileSync(filePath, 'utf-8');

const clean = raw
.replace(/<\?php\s*/g, '')
.replace(/return\s*\[/, '')
.replace(/\];/, '')
.trim();

return clean
.split('\n')
.map(line => line.trim())
.filter(line => line.includes('=>') && !line.startsWith('//'))
.map(line => {
// Extract the key before =>
const match = line.match(/^['"](.+?)['"]\s*=>/);
return match ? match[1] : null;
})
.filter(Boolean) as string[];
}
test('All language files must match number of keys and key names with English app.php', () => {
const baseFile = path.join(LANG_DIR, BASE_LANG, 'app.php');
const baseKeys = getNormalizedKeys(baseFile);
// All locales except the base one
const locales = fs
.readdirSync(LANG_DIR)
.filter(locale => locale !== BASE_LANG && fs.existsSync(path.join(LANG_DIR, locale, 'app.php')));

// Array to collect any locales that have issues
let failedLocales: { locale: string; missingKeys: string[]; extraKeys: string[] }[] = [];

for (const locale of locales) {
const filePath = path.join(LANG_DIR, locale, 'app.php');
const keys = getNormalizedKeys(filePath);

const missingKeys = baseKeys.filter(key => !keys.includes(key));
const extraKeys = keys.filter(key => !baseKeys.includes(key));

if (missingKeys.length > 0 || extraKeys.length > 0) {
failedLocales.push({ locale, missingKeys, extraKeys });
}
}

if (failedLocales.length > 0) {
for (const { locale, missingKeys, extraKeys } of failedLocales) {
console.error(` ${locale}/app.php has issues:`);

if (missingKeys.length) {
console.error(` Missing keys (${missingKeys.length}):`);
for (const key of missingKeys) console.error(` - ${key}`);
}

if (extraKeys.length) {
console.error(` Extra keys (${extraKeys.length}):`);
for (const key of extraKeys) console.error(` + ${key}`);
}
}

// Fail the test
expect(failedLocales).toEqual([]);
} else {
console.log('All language files have matching keys and counts with en/app.php');
}
});
Loading