-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathupdate-en-comments
executable file
·107 lines (78 loc) · 2.31 KB
/
update-en-comments
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
#!/usr/bin/env php
<?php declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
require __DIR__ . '/../vendor/codeigniter4/framework/system/Test/bootstrap.php';
use CodeIgniter\CLI\CLI;
helper('filesystem');
if ($argc !== 2) {
CLI::error('Please specify a locale.');
exit(1);
}
$locale = $argv[1];
$langDir = realpath(__DIR__ . '/../src/Language/' . $locale);
if (! is_dir($langDir)) {
CLI::error('No such directory: "' . $langDir . '"');
exit(1);
}
$enDir = realpath(__DIR__ . '/../src/Language/en');
if (! is_dir($enDir)) {
CLI::error('No "Language/en" directory. Please run "composer update".');
exit(1);
}
$files = get_filenames(
$langDir,
true,
false,
false
);
$enFiles = get_filenames(
$enDir,
true,
false,
false
);
foreach ($enFiles as $enFile) {
$temp = $langDir . '/' . substr($enFile, strlen($enDir) + 1);
$langFile = realpath($temp) ?: $temp;
if (! is_file($langFile)) {
CLI::error('No such file: "' . $langFile . '"');
continue;
}
$enFileLines = file($enFile);
$items = [];
$pattern = '/(.*)\'([a-zA-Z0-9_]+?)\'(\s*=>\s*)([\'"].+[\'"]),/u';
foreach ($enFileLines as $line) {
if (preg_match($pattern, $line, $matches)) {
$items[] = [$matches[2] => $matches[4]];
}
}
$langFileLines = file($langFile);
$newLangFile = '';
$itemNo = 0;
foreach ($langFileLines as $line) {
// Remove en value comment.
if (preg_match('!(.*,)(\s*//.*)$!u', $line, $matches)) {
$line = $matches[1] . "\n";
}
if (preg_match($pattern, $line, $matches) === 0) {
$newLangFile .= $line;
} else {
$indent = $matches[1];
$key = $matches[2];
$arrow = $matches[3];
$value = $matches[4];
$newLangFile .= $indent . "'" . $key . "'" . $arrow . $value
. ', // ' . $items[$itemNo][array_key_first($items[$itemNo])] . "\n";
$itemNo++;
}
}
file_put_contents($langFile, $newLangFile);
CLI::write('Updated: ' . $langFile);
}