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
|
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/_cli.php';
require_once __DIR__ . '/i18n/I18nData.php';
require_once __DIR__ . '/i18n/I18nFile.php';
require_once dirname(__DIR__) . '/constants.php';
$cliOptions = new class extends CliOptionsParser {
public string $action;
public string $key;
public string $newKey;
public string $value;
public string $language;
public string $originLanguage;
public bool $revert;
public bool $help;
public function __construct() {
$this->addRequiredOption('action', (new CliOption('action', 'a')));
$this->addOption('key', (new CliOption('key', 'k')));
$this->addOption('newKey', (new CliOption('new-key', 'n')));
$this->addOption('value', (new CliOption('value', 'v')));
$this->addOption('language', (new CliOption('language', 'l')));
$this->addOption('originLanguage', (new CliOption('origin-language', 'o')));
$this->addOption('revert', (new CliOption('revert', 'r'))->withValueNone());
$this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
parent::__construct();
}
};
if (!empty($cliOptions->errors)) {
fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
}
if ($cliOptions->help) {
manipulateHelp();
}
$data = new I18nFile();
$i18nData = new I18nData($data->load());
switch ($cliOptions->action) {
case 'add':
if (isset($cliOptions->key) && isset($cliOptions->value) && isset($cliOptions->language)) {
$i18nData->addValue($cliOptions->key, $cliOptions->value, $cliOptions->language);
} elseif (isset($cliOptions->key) && isset($cliOptions->value)) {
$i18nData->addKey($cliOptions->key, $cliOptions->value);
} elseif (isset($cliOptions->key)) {
$i18nData->addFile($cliOptions->key);
} elseif (isset($cliOptions->language)) {
$reference = null;
if (isset($cliOptions->originLanguage)) {
$reference = $cliOptions->originLanguage;
}
$i18nData->addLanguage($cliOptions->language, $reference);
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
case 'move':
if (isset($cliOptions->key) && isset($cliOptions->newKey)) {
$i18nData->moveKey($cliOptions->key, $cliOptions->newKey);
} else {
error('You need to specify the key to move and its new location.');
exit;
}
break;
case 'delete':
if (isset($cliOptions->key)) {
$i18nData->removeKey($cliOptions->key);
} else {
error('You need to specify the key to delete.');
exit;
}
break;
case 'exist':
if (isset($cliOptions->key)) {
$key = $cliOptions->key;
if ($i18nData->isKnown($key)) {
echo "The '{$key}' key is known.\n\n";
} else {
echo "The '{$key}' key is unknown.\n\n";
}
} else {
error('You need to specify the key to check.');
exit;
}
break;
case 'format':
break;
case 'ignore':
if (isset($cliOptions->language) && isset($cliOptions->key)) {
$i18nData->ignore($cliOptions->key, $cliOptions->language, $cliOptions->revert);
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
case 'ignore_unmodified':
if (isset($cliOptions->language)) {
$i18nData->ignore_unmodified($cliOptions->language, $cliOptions->revert);
} else {
error('You need to specify a valid set of options.');
exit;
}
break;
default:
manipulateHelp();
exit;
}
$data->dump($i18nData->getData());
/**
* Output error message.
*/
function error(string $message): void {
$error = <<<ERROR
WARNING
%s\n\n
ERROR;
echo sprintf($error, $message);
manipulateHelp();
}
/**
* Output help message.
*/
function manipulateHelp(): void {
$file = str_replace(__DIR__ . '/', '', __FILE__);
echo <<<HELP
NAME
$file
SYNOPSIS
php $file [OPTIONS]
DESCRIPTION
Manipulate translation files.
-a, --action=ACTION
select the action to perform. Available actions are add, move, delete,
exist, format, ignore, and ignore_unmodified. This option is mandatory.
-k, --key=KEY select the key to work on.
-v, --value=VAL select the value to set.
-l, --language=LANG select the language to work on.
-h, --help display this help and exit.
-r, --revert revert the action (only for ignore action)
-o, origin-language=LANG
select the origin language (only for add language action)
EXAMPLES
Example 1: add a language. Adds a new language by duplicating the reference language.
php $file -a add -l my_lang
php $file -a add -l my_lang -o ref_lang
Example 2: add a new key. Adds a key to all supported languages.
php $file -a add -k my_key -v my_value
Example 3: add a new value. Sets a new value for the selected key in the selected language.
php $file -a add -k my_key -v my_value -l my_lang
Example 4: delete a key. Deletes the selected key from all supported languages.
php $file -a delete -k my_key
Example 5: format i18n files.
php $file -a format
Example 6: ignore a key. Adds IGNORE comment to the key in the selected language, marking it as translated.
php $file -a ignore -k my_key -l my_lang
Example 7: revert ignore a key. Removes IGNORE comment from the key in the selected language.
php $file -a ignore -r -k my_key -l my_lang
Example 8: ignore all unmodified keys. Adds IGNORE comments to all unmodified keys in the selected language, marking them as translated.
php $file -a ignore_unmodified -l my_lang
Example 9: revert ignore on all unmodified keys. Removes IGNORE comments from all unmodified keys in the selected language.
Warning: will also revert individually added IGNORE(s) on unmodified keys.
php $file -a ignore_unmodified -r -l my_lang
Example 10: check if a key exist.
php $file -a exist -k my_key
Example 11: add a new file to all languages
php $file -a add -k my_file.php
Example 12:\tmove an existing key into a new location
php $file -a move -k my_key -n new_location
HELP, PHP_EOL;
exit();
}
|