-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_lang_files.php
More file actions
1562 lines (1334 loc) · 45.3 KB
/
test_lang_files.php
File metadata and controls
1562 lines (1334 loc) · 45.3 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Test script to verify the correct formatting of phpgw_*.lang files
* and check for missing translations between language files
*
* Format should be: key<tab>module<tab>lang<tab>value
*
* Usage:
* php test_lang_files.php # Check all language files
* php test_lang_files.php --verbose # Show detailed information about issues
* php test_lang_files.php --lang=no,en # Check only Norwegian and English files
* php test_lang_files.php --output=json # Output results in JSON format for parsing
* php test_lang_files.php --compare # Compare languages for missing translations
* php test_lang_files.php --modules=booking,property # Check specific modules only
* php test_lang_files.php --module=booking # Alternative syntax for modules
* php test_lang_files.php --compare --modules=booking,property # Compare langs in specific modules
* php test_lang_files.php --compare --baseline=en # Use English as baseline for comparison
* php test_lang_files.php --search=module.key --langs=no,en # Search for module.key in specified languages
* php test_lang_files.php --add-translation --key=key --module=module --langs=no:text,en:text # Add new translations
*/
// Configuration
$base_dir = __DIR__; // Base directory to start the search
$errors = [];
$files_checked = 0;
$lines_checked = 0;
$error_categories = [
'wrong_tab_count' => 0,
'extra_tabs' => 0,
'spaces_instead_of_tabs' => 0,
'empty_fields' => 0,
'other' => 0,
];
// Parse command line options
$help_requested = in_array('--help', $argv) || in_array('-h', $argv);
$verbose = in_array('--verbose', $argv) || in_array('-v', $argv);
$json_output = in_array('--output=json', $argv);
$compare_mode = in_array('--compare', $argv);
$sort_mode = in_array('--sort', $argv);
$search_mode = false;
$search_key = '';
$search_value_mode = false;
$search_value = '';
$add_translation_mode = in_array('--add-translation', $argv);
$translation_key = '';
$translation_module = '';
$translation_langs = []; // Will be in format ['en' => 'English text', 'no' => 'Norwegian text']
$langs = [];
$modules = [];
// Debug code removed
$baseline_lang = 'en'; // Default baseline language for comparison
// Parse more arguments
foreach ($argv as $arg)
{
if (strpos($arg, '--lang=') === 0)
{
$lang_list = substr($arg, 7); // Remove '--lang='
$langs = array_map('trim', explode(',', $lang_list));
}
elseif (strpos($arg, '--langs=') === 0)
{
$lang_list = substr($arg, 8); // Remove '--langs='
$langs = array_map('trim', explode(',', $lang_list));
}
elseif (strpos($arg, '--modules=') === 0)
{
$module_list = substr($arg, 10); // Remove '--modules='
$modules = array_map('trim', explode(',', $module_list));
}
elseif (strpos($arg, '--module=') === 0)
{
$module_value = substr($arg, 9); // Remove '--module='
if ($add_translation_mode) {
// In add-translation mode, use this as the translation module
$translation_module = $module_value;
} else {
// In standard mode, treat it as a module list
$modules = array_map('trim', explode(',', $module_value));
}
}
elseif (strpos($arg, '--baseline=') === 0)
{
$baseline_lang = substr($arg, 11); // Remove '--baseline='
}
elseif (strpos($arg, '--search=') === 0)
{
$search_key = substr($arg, 9); // Remove '--search='
$search_mode = true;
}
elseif (strpos($arg, '--search-value=') === 0)
{
$search_value = substr($arg, 15); // Remove '--search-value='
$search_value_mode = true;
}
elseif (strpos($arg, '--key=') === 0)
{
$translation_key = substr($arg, 6); // Remove '--key='
}
elseif (strpos($arg, '--langs=') === 0)
{
$lang_list = substr($arg, 8); // Remove '--langs='
if (!$add_translation_mode) {
// For normal mode (not add-translation)
$langs = array_map('trim', explode(',', $lang_list));
}
}
}
// Display help if requested
if ($help_requested)
{
echo "Language File Validator and Comparison Tool\n";
echo "=========================================\n\n";
echo "This tool validates the format of phpgw_*.lang files and can compare translations between languages.\n";
echo "Each line should follow the format: key<tab>module<tab>lang<tab>value\n\n";
echo "Usage:\n";
echo " php " . basename(__FILE__) . " [options]\n\n";
echo "Options:\n";
echo " --help, -h Display this help message\n";
echo " --verbose, -v Show more detailed information about the issues\n";
echo " --output=json Output results in JSON format for parsing\n";
echo " --lang=xx,yy,zz Only check files for specific languages (comma-separated)\n";
echo " Example: --lang=no,en,de\n";
echo " --langs=xx,yy,zz Alternative syntax for --lang\n";
echo " --compare Compare languages for missing translations\n";
echo " --modules=a,b,c Only check specified modules (in both modes)\n";
echo " --module=a,b,c Alternative syntax for --modules\n";
echo " --baseline=xx Use specified language as baseline for comparison (default: en)\n";
echo " --sort Sort language files alphabetically by key and deduplicate entries\n";
echo " --search=module.key Search for a specific translation key in format module.key\n";
echo " Example: --search=booking.save\n";
echo " --search-value=text Search for translation values containing the specified text\n";
echo " Example: --search-value=\"booking has been registered\"\n";
echo " --add-translation Add new translations to language files\n";
echo " --key=key The key to add (used with --add-translation)\n";
echo " --module=module The module for the translation (used with --add-translation)\n";
echo " Use 'common' for global translations (phpgwapi)\n";
echo " --langs=lang:text Language and translation pairs (used with --add-translation)\n";
echo " Format: lang1:text1,lang2:text2\n";
echo " Basic example: --langs=no:Tekst,en:Text\n";
echo " For text with commas, spaces, or special characters, use quotes:\n";
echo " Example: --langs=\"no:Tekst med komma, og mellomrom,en:Text with comma, and spaces\"\n\n";
echo "Examples:\n";
echo " php " . basename(__FILE__) . " # Check all language files\n";
echo " php " . basename(__FILE__) . " --verbose # Show detailed diagnostic information\n";
echo " php " . basename(__FILE__) . " --lang=no,en # Check only Norwegian and English files\n";
echo " php " . basename(__FILE__) . " --module=booking # Check only files in booking module\n";
echo " php " . basename(__FILE__) . " --output=json # Output in JSON format for processing\n";
echo " php " . basename(__FILE__) . " --compare --lang=no,en,nn # Find missing translations between languages\n";
echo " php " . basename(__FILE__) . " --compare --modules=booking,property # Compare only specific modules\n";
echo " php " . basename(__FILE__) . " --compare --module=booking # Compare only a single module\n";
echo " php " . basename(__FILE__) . " --compare --baseline=no # Use Norwegian as baseline for comparison\n";
echo " php " . basename(__FILE__) . " --sort --lang=en,no,nn --module=booking # Sort and deduplicate booking module language files\n";
echo " php " . basename(__FILE__) . " --search=booking.save --langs=no,en,nn # Search for 'booking.save' key in specified languages\n";
echo " php " . basename(__FILE__) . " --search-value=\"booking has been registered\" --langs=no,en,nn # Search for translation values\n";
echo " php " . basename(__FILE__) . " --add-translation --key=save --module=booking --langs=no:Lagre,en:Save,nn:Lagra # Add translations\n";
echo " php " . basename(__FILE__) . " --add-translation --key=confirm --module=booking --langs=\"no:Er du sikker?,en:Are you sure?\" # With special characters\n";
exit(0);
}
// No post-processing needed anymore
// Show which languages are being checked (except in add-translation mode)
if (!empty($langs) && !$add_translation_mode)
{
echo "Filtering to only check languages: " . implode(', ', $langs) . "\n";
}
// If modules specified (except in add-translation mode)
if (!empty($modules) && !$add_translation_mode)
{
echo "Filtering to only check modules: " . implode(', ', $modules) . "\n";
}
if ($compare_mode)
{
echo "Using '" . $baseline_lang . "' as baseline language for comparison\n";
}
// Function to check a language file
function check_lang_file($file_path)
{
global $errors, $lines_checked, $error_categories;
$content = file_get_contents($file_path);
$lines = explode("\n", $content);
$line_number = 0;
foreach ($lines as $line)
{
$line_number++;
$original_line = $line;
$line = trim($line);
// Skip empty lines and comments
if (empty($line) || strpos($line, '#') === 0)
{
continue;
}
$lines_checked++;
// Check if spaces are used instead of tabs
if (strpos($line, ' ') !== false && strpos($line, "\t") === false)
{
// Spaces instead of tabs issue
$error = [
'file' => $file_path,
'line' => $line_number,
'content' => $line,
'error' => 'Using spaces instead of tabs',
'category' => 'spaces_instead_of_tabs',
'severity' => 'error'
];
$errors[] = $error;
$error_categories['spaces_instead_of_tabs']++;
continue;
}
// Split by tab character
$parts = explode("\t", $line);
// Check if we have exactly 4 parts
if (count($parts) !== 4)
{
$error_type = count($parts) < 4 ? 'wrong_tab_count' : 'extra_tabs';
$error = [
'file' => $file_path,
'line' => $line_number,
'content' => $line,
'part_count' => count($parts),
'error' => 'Expected 4 tab-separated parts (key, module, lang, value), found ' . count($parts),
'category' => $error_type,
'severity' => 'error'
];
$errors[] = $error;
$error_categories[$error_type]++;
continue;
}
// Check that each part is not empty
$empty_fields = [];
foreach ($parts as $index => $part)
{
if (trim($part) === '')
{
$field_names = ['key', 'module', 'lang', 'value'];
$empty_fields[] = $field_names[$index];
}
}
if (!empty($empty_fields))
{
$errors[] = [
'file' => $file_path,
'line' => $line_number,
'content' => $line,
'error' => "Empty fields: " . implode(', ', $empty_fields),
'category' => 'empty_fields',
'severity' => 'error',
'empty_fields' => $empty_fields
];
$error_categories['empty_fields']++;
}
// Check for whitespace issues at the beginning/end of fields
$has_whitespace_issues = false;
$whitespace_issues = [];
foreach ($parts as $index => $part)
{
$field_names = ['key', 'module', 'lang', 'value'];
if ($part !== trim($part))
{
$has_whitespace_issues = true;
$whitespace_issues[] = $field_names[$index];
}
}
if ($has_whitespace_issues)
{
$errors[] = [
'file' => $file_path,
'line' => $line_number,
'content' => $line,
'error' => "Extra whitespace in fields: " . implode(', ', $whitespace_issues),
'category' => 'other',
'severity' => 'warning',
'whitespace_issues' => $whitespace_issues
];
$error_categories['other']++;
}
}
}
// Find all phpgw_*.lang files recursively or directly for specific modules
function find_lang_files($dir)
{
global $files_checked, $langs, $modules, $verbose;
$results = [];
// Special handling for module filtering
if (!empty($modules)) {
// Direct module path lookup
foreach ($modules as $module) {
// Special handling for "common" module - look in phpgwapi
$actual_module = ($module === "common") ? "phpgwapi" : $module;
$module_setup_dir = $dir . "/src/modules/" . $actual_module . "/setup";
if ($verbose) {
echo "Checking module directory: {$module_setup_dir}\n";
if ($module === "common") {
echo " (Using phpgwapi for 'common' module)\n";
}
}
if (is_dir($module_setup_dir)) {
$module_files = scandir($module_setup_dir);
foreach ($module_files as $file) {
if (preg_match('/phpgw_(.*)\.lang$/', $file, $matches)) {
$lang_code = $matches[1];
$path = $module_setup_dir . "/" . $file;
// Filter by language if specified
if (!empty($langs) && !in_array($lang_code, $langs)) {
if ($verbose) {
echo " Skipping non-matching language: {$lang_code}\n";
}
continue;
}
if ($verbose) {
echo " Found language file: {$path}\n";
}
$results[] = $path;
$files_checked++;
}
}
} else if ($verbose) {
echo " Module directory not found: {$module_setup_dir}\n";
}
}
return $results;
}
// Regular recursive search for all modules
if ($verbose) {
echo "Scanning directory: {$dir}\n";
}
$files = scandir($dir);
foreach ($files as $file)
{
if ($file === '.' || $file === '..') continue;
$path = $dir . '/' . $file;
if (is_dir($path))
{
// Skip the 'out' directory as it contains compiled output
if (basename($path) === 'out') {
if ($verbose) {
echo " Skipping 'out' directory: {$path}\n";
}
continue;
}
// For better performance, skip some common non-module directories
if (in_array(basename($path), ['.git', 'vendor', 'node_modules'])) {
if ($verbose) {
echo " Skipping directory: {$path}\n";
}
continue;
}
$results = array_merge($results, find_lang_files($path));
} elseif (preg_match('/phpgw_(.*)\.lang$/', $file, $matches))
{
$lang_code = $matches[1];
if ($verbose) {
echo " Found language file: {$path} (language: {$lang_code})\n";
}
// Filter by language if specified
if (!empty($langs) && !in_array($lang_code, $langs))
{
if ($verbose) {
echo " Skipping non-matching language: {$lang_code}\n";
}
continue;
}
if ($verbose) {
echo " Adding to results: {$path}\n";
}
$results[] = $path;
$files_checked++;
}
}
return $results;
}
// Function to search for a specific key in module.key format
function search_for_key($lang_files, $search_key)
{
global $verbose, $base_dir, $langs;
// Split the search key into module and key parts
if (strpos($search_key, '.') !== false) {
list($search_module, $search_phrase) = explode('.', $search_key, 2);
// Handle "common" module special case
if ($search_module === 'common') {
if ($verbose) {
echo "Converting search module from 'common' to 'phpgwapi' internally\n";
}
}
} else {
// If no dot is present, treat the entire string as the key and search in all modules
$search_module = null;
$search_phrase = $search_key;
}
if ($verbose) {
echo "Searching for ";
if ($search_module) {
echo "module: '{$search_module}', ";
}
echo "key: '{$search_phrase}'\n";
}
$results = [];
$total_matches = 0;
foreach ($lang_files as $file_path) {
// Extract language from file path
if (preg_match('/phpgw_(.*)\.lang$/', basename($file_path), $matches)) {
$lang_code = $matches[1];
} else {
$lang_code = 'unknown';
}
// Extract module from the file path
$module = get_module_from_path($file_path);
// Skip if we're searching for a specific module and this isn't it
if ($search_module !== null) {
// Special handling for "common" module
if (($search_module === 'common' && $module !== 'common') ||
($search_module !== 'common' && $search_module !== $module)) {
if ($verbose) {
echo "Skipping {$file_path}, module '{$module}' doesn't match search module '{$search_module}'\n";
}
continue;
}
}
$content = file_get_contents($file_path);
$lines = explode("\n", $content);
$matches = [];
foreach ($lines as $line_number => $line) {
$line = trim($line);
// Skip empty lines and comments
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
$parts = explode("\t", $line);
if (count($parts) === 4) {
$key = $parts[0];
$module_in_file = $parts[1];
$lang = $parts[2];
$value = $parts[3];
// Only match if the module matches our search (if specified)
if ($search_module !== null && $search_module !== $module_in_file) {
continue;
}
// Check if key matches search phrase
if ($key === $search_phrase) {
$matches[] = [
'line_number' => $line_number + 1, // 1-based line numbers
'key' => $key,
'module' => $module_in_file,
'lang' => $lang,
'value' => $value,
'full_line' => $line
];
}
}
}
if (!empty($matches)) {
$relative_path = str_replace($base_dir, '', $file_path);
$results[$file_path] = [
'file' => $relative_path,
'language' => $lang_code,
'module' => $module,
'matches' => $matches,
'match_count' => count($matches)
];
$total_matches += count($matches);
}
}
return [
'results' => $results,
'total_matches' => $total_matches,
'search_module' => $search_module,
'search_key' => $search_phrase
];
}
// Function to search for translation values containing specific text
function search_for_value($lang_files, $search_value)
{
global $verbose, $base_dir, $langs;
if ($verbose) {
echo "Searching for values containing: '{$search_value}'\n";
}
$results = [];
$total_matches = 0;
foreach ($lang_files as $file_path) {
// Extract language from file path
if (preg_match('/phpgw_(.*)\.lang$/', basename($file_path), $matches)) {
$lang_code = $matches[1];
} else {
$lang_code = 'unknown';
}
// Extract module from the file path
$module = get_module_from_path($file_path);
$content = file_get_contents($file_path);
$lines = explode("\n", $content);
$matches = [];
foreach ($lines as $line_number => $line) {
$line = trim($line);
// Skip empty lines and comments
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
$parts = explode("\t", $line);
if (count($parts) === 4) {
$key = $parts[0];
$module_in_file = $parts[1];
$lang = $parts[2];
$value = $parts[3];
// Check if value contains the search text (case-insensitive)
if (stripos($value, $search_value) !== false) {
$matches[] = [
'line_number' => $line_number + 1, // 1-based line numbers
'key' => $key,
'module' => $module_in_file,
'lang' => $lang,
'value' => $value,
'full_line' => $line
];
}
}
}
if (!empty($matches)) {
$relative_path = str_replace($base_dir, '', $file_path);
$results[$file_path] = [
'file' => $relative_path,
'language' => $lang_code,
'module' => $module,
'matches' => $matches,
'match_count' => count($matches)
];
$total_matches += count($matches);
}
}
return [
'results' => $results,
'total_matches' => $total_matches,
'search_value' => $search_value
];
}
// Function to extract module name from file path
function get_module_from_path($file_path)
{
global $verbose;
if (preg_match('#/modules/([^/]+)/#', $file_path, $matches)) {
$module = $matches[1];
// Special handling for phpgwapi - return as "common"
if ($module === 'phpgwapi') {
$module = 'common';
}
if ($verbose) {
echo "Extracted module '{$module}' from path '{$file_path}'\n";
if ($module === 'common') {
echo " (Converted from 'phpgwapi' to 'common')\n";
}
}
return $module;
}
if ($verbose) {
echo "Could not extract module from path '{$file_path}'\n";
}
return 'unknown';
}
// Function to add new translations to language files
function add_translation($translation_key, $translation_module, $translation_langs)
{
global $base_dir, $verbose;
if (empty($translation_key)) {
echo "Error: Translation key is required. Use --key=your_key\n";
exit(1);
}
if (empty($translation_module)) {
echo "Error: Module is required. Use --module=module_name\n";
exit(1);
}
if (empty($translation_langs)) {
echo "Error: No translations provided. Use --langs=lang:text format\n";
echo "Example: --langs=no:Norwegian text,en:English text\n";
exit(1);
}
echo "Adding translations for key '{$translation_key}' in module '{$translation_module}'...\n";
// For "common" module, use phpgwapi
$module_path = $translation_module;
if ($translation_module === "common") {
$module_path = "phpgwapi";
}
// Find the module directory
$module_setup_dir = $base_dir . "/src/modules/" . $module_path . "/setup";
if (!is_dir($module_setup_dir)) {
echo "Error: Module directory not found: {$module_setup_dir}\n";
exit(1);
}
$success_count = 0;
$error_count = 0;
foreach ($translation_langs as $lang_code => $translation_text) {
$lang_file = $module_setup_dir . "/phpgw_" . $lang_code . ".lang";
if (!file_exists($lang_file)) {
echo "Warning: Language file for '{$lang_code}' does not exist. Creating new file...\n";
// Create file with header comment
$content = "# {$translation_module} language file for {$lang_code}\n";
file_put_contents($lang_file, $content);
if (!file_exists($lang_file)) {
echo "Error: Could not create language file: {$lang_file}\n";
$error_count++;
continue;
}
}
// Check if key already exists
$current_content = file_get_contents($lang_file);
$lines = explode("\n", $current_content);
$key_exists = false;
foreach ($lines as $line) {
$line = trim($line);
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
$parts = explode("\t", $line);
if (count($parts) === 4 && $parts[0] === $translation_key && $parts[1] === $translation_module) {
$key_exists = true;
if ($verbose) {
echo "Key '{$translation_key}' for module '{$translation_module}' already exists in {$lang_file}.\n";
echo "Current value: \"{$parts[3]}\"\n";
echo "New value: \"{$translation_text}\"\n";
}
break;
}
}
if ($key_exists) {
echo "Warning: Translation for key '{$translation_key}' in module '{$translation_module}' already exists in {$lang_code}. Skipping...\n";
$error_count++;
continue;
}
// Prepare new entry
$new_entry = $translation_key . "\t" . $translation_module . "\t" . $lang_code . "\t" . $translation_text;
// Append to file
$result = file_put_contents($lang_file, $current_content . ($current_content ? "\n" : "") . $new_entry);
if ($result === false) {
echo "Error: Failed to add translation for '{$lang_code}' to {$lang_file}\n";
$error_count++;
} else {
echo "Success: Added translation for '{$lang_code}': {$translation_key} => \"{$translation_text}\"\n";
$success_count++;
}
}
echo "\nCompleted adding translations:\n";
echo " - Successfully added: {$success_count}\n";
if ($error_count > 0) {
echo " - Failed to add: {$error_count}\n";
exit(1);
}
return true;
}
// Function to extract language entries from a file
function extract_lang_entries($file_path)
{
$entries = [];
$content = file_get_contents($file_path);
$lines = explode("\n", $content);
foreach ($lines as $line) {
$line = trim($line);
// Skip empty lines and comments
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
$parts = explode("\t", $line);
if (count($parts) === 4) {
$key = $parts[0];
$module = $parts[1];
$entries["$key|$module"] = [
'key' => $key,
'module' => $module,
'lang' => $parts[2],
'value' => $parts[3],
'original_line' => $line
];
}
}
return $entries;
}
// Function to sort and save a language file
function sort_lang_file($file_path)
{
global $verbose;
// Read the file content
$content = file_get_contents($file_path);
if ($content === false) {
echo "Error: Could not read file {$file_path}\n";
return false;
}
// Split into lines
$lines = explode("\n", $content);
$entries = [];
$comments = [];
$empty_lines = [];
$duplicates = [];
// Extract all entries, preserving comments and empty lines
foreach ($lines as $line_number => $line) {
$trimmed = trim($line);
if (empty($trimmed)) {
$empty_lines[$line_number] = $line;
} elseif (strpos($trimmed, '#') === 0) {
$comments[$line_number] = $line;
} else {
$parts = explode("\t", $line);
if (count($parts) === 4) {
$key = $parts[0];
$module = $parts[1];
$lang = $parts[2];
$value = $parts[3];
// Create a unique identifier for detecting duplicates
$entry_id = $key . '|' . $module;
if (isset($entries[$entry_id])) {
// We found a duplicate!
if (!isset($duplicates[$entry_id])) {
$duplicates[$entry_id] = 1;
}
$duplicates[$entry_id]++;
if ($verbose) {
echo "Found duplicate entry in {$file_path} at line {$line_number}: {$key} (module: {$module})\n";
}
}
// Always use the last occurrence of a duplicate key
$entries[$entry_id] = [
'key' => $key,
'module' => $module,
'lang' => $lang,
'value' => $value,
'original_line' => $line
];
} else {
// Skip malformed lines
if ($verbose) {
echo "Warning: Skipping malformed line {$line_number} in {$file_path}\n";
}
}
}
}
// Report on duplicates found
$duplicate_count = count($duplicates);
if ($duplicate_count > 0) {
echo "Found {$duplicate_count} duplicate entries in {$file_path} (keeping only the last occurrence)\n";
if ($verbose) {
foreach ($duplicates as $entry_id => $count) {
list($key, $module) = explode('|', $entry_id, 2);
echo " - '{$key}' (module: {$module}) appeared {$count} times\n";
}
}
}
// Convert to indexed array for sorting
$entries_array = array_values($entries);
// Sort entries by key
usort($entries_array, function ($a, $b) {
return strcasecmp($a['key'], $b['key']);
});
// Rebuild the file content
$sorted_lines = [];
foreach ($entries_array as $entry) {
$sorted_lines[] = $entry['key'] . "\t" . $entry['module'] . "\t" . $entry['lang'] . "\t" . $entry['value'];
}
// Write back to file
$new_content = implode("\n", $sorted_lines);
if (file_put_contents($file_path, $new_content) === false) {
echo "Error: Could not write to file {$file_path}\n";
return false;
}
$result_message = "Successfully sorted {$file_path} by key";
if ($duplicate_count > 0) {
$result_message .= " (removed {$duplicate_count} duplicates)";
}
if ($verbose || $duplicate_count > 0) {
echo "{$result_message}\n";
}
return true;
}
// Function to compare language files for missing translations
function compare_lang_files($lang_files)
{
global $modules, $baseline_lang, $verbose;
// Group files by module and language
$files_by_module_and_lang = [];
foreach ($lang_files as $file_path) {
$module = get_module_from_path($file_path);
// Skip if not in specified modules (when modules filter is active)
if (!empty($modules)) {
$module_match = false;
foreach ($modules as $requested_module) {
// Handle "common" module special case
if (($requested_module === 'common' && $module === 'common') ||
($requested_module !== 'common' && $module === $requested_module)) {
$module_match = true;
break;
}
}
if (!$module_match) {
continue;
}
}
if (preg_match('/phpgw_(.*)\.lang$/', basename($file_path), $matches)) {
$lang = $matches[1];
$files_by_module_and_lang[$module][$lang] = $file_path;
}
}
// Check for missing translations
$missing_translations = [];
$total_missing = 0;
foreach ($files_by_module_and_lang as $module => $lang_files) {
// Skip if baseline language is missing for this module
if (!isset($lang_files[$baseline_lang])) {
echo "Warning: Baseline language '$baseline_lang' not found for module '$module', skipping comparison\n";
continue;
}
// Extract entries from baseline language
$baseline_entries = extract_lang_entries($lang_files[$baseline_lang]);
$baseline_count = count($baseline_entries);
foreach ($lang_files as $lang => $file_path) {
if ($lang === $baseline_lang) continue; // Skip baseline language
$target_entries = extract_lang_entries($file_path);
$target_count = count($target_entries);
// Find entries in baseline that are missing in target language
$missing = [];
foreach ($baseline_entries as $key => $entry) {
if (!isset($target_entries[$key])) {
$missing[] = [
'key' => $entry['key'],
'module' => $entry['module'],
'baseline_value' => $entry['value']
];
}
}
// Find entries in target that are not in baseline (extra translations)
$extra = [];
foreach ($target_entries as $key => $entry) {
if (!isset($baseline_entries[$key])) {
$extra[] = [
'key' => $entry['key'],
'module' => $entry['module'],
'target_value' => $entry['value']
];
}
}
$missing_count = count($missing);
$extra_count = count($extra);
$total_missing += $missing_count;
if ($missing_count > 0 || $extra_count > 0 || $verbose) {
$missing_translations[$module][$lang] = [
'file' => $file_path,
'baseline_count' => $baseline_count,
'target_count' => $target_count,
'missing' => $missing,
'missing_count' => $missing_count,
'extra' => $extra,
'extra_count' => $extra_count
];
}
}
}
return [
'missing_translations' => $missing_translations,
'total_missing' => $total_missing
];
}
// Find and check all lang files
echo "Starting validation of language files...\n";
$lang_files = find_lang_files($base_dir);
if (empty($lang_files) && !$add_translation_mode)
{
echo "No language files found! Please check the base directory.\n";
exit(1);
}
// ADD TRANSLATION MODE
if ($add_translation_mode) {
// Manually parse arguments
foreach ($argv as $arg) {