4
0

manipulate.translation.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require_once __DIR__ . '/_cli.php';
  5. require_once __DIR__ . '/i18n/I18nData.php';
  6. require_once __DIR__ . '/i18n/I18nFile.php';
  7. require_once dirname(__DIR__) . '/constants.php';
  8. $cliOptions = new class extends CliOptionsParser {
  9. public string $action;
  10. public string $key;
  11. public string $newKey;
  12. public string $value;
  13. public string $language;
  14. public string $originLanguage;
  15. public bool $revert;
  16. public bool $help;
  17. public function __construct() {
  18. $this->addRequiredOption('action', (new CliOption('action', 'a')));
  19. $this->addOption('key', (new CliOption('key', 'k')));
  20. $this->addOption('newKey', (new CliOption('new-key', 'n')));
  21. $this->addOption('value', (new CliOption('value', 'v')));
  22. $this->addOption('language', (new CliOption('language', 'l')));
  23. $this->addOption('originLanguage', (new CliOption('origin-language', 'o')));
  24. $this->addOption('revert', (new CliOption('revert', 'r'))->withValueNone());
  25. $this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
  26. parent::__construct();
  27. }
  28. };
  29. if (!empty($cliOptions->errors)) {
  30. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  31. }
  32. if ($cliOptions->help) {
  33. manipulateHelp();
  34. }
  35. $data = new I18nFile();
  36. $i18nData = new I18nData($data->load());
  37. switch ($cliOptions->action) {
  38. case 'add':
  39. if (($cliOptions->key ?? '') !== '' && ($cliOptions->value ?? '') !== '' && ($cliOptions->language ?? '') !== '') {
  40. $i18nData->addValue($cliOptions->key, $cliOptions->value, $cliOptions->language);
  41. } elseif (($cliOptions->key ?? '') !== '' && ($cliOptions->value ?? '') !== '') {
  42. $i18nData->addKey($cliOptions->key, $cliOptions->value);
  43. } elseif (($cliOptions->key ?? '') !== '') {
  44. $i18nData->addFile($cliOptions->key);
  45. } elseif (($cliOptions->language ?? '') !== '') {
  46. $reference = ($cliOptions->originLanguage ?? '') === '' ? null : $cliOptions->originLanguage;
  47. $i18nData->addLanguage($cliOptions->language, $reference);
  48. } else {
  49. error('You need to specify a valid set of options.');
  50. exit;
  51. }
  52. break;
  53. case 'move':
  54. if (($cliOptions->key ?? '') !== '' && ($cliOptions->newKey ?? '') !== '') {
  55. $i18nData->moveKey($cliOptions->key, $cliOptions->newKey);
  56. } else {
  57. error('You need to specify the key to move and its new location.');
  58. exit;
  59. }
  60. break;
  61. case 'delete':
  62. if (($cliOptions->key ?? '') !== '') {
  63. $i18nData->removeKey($cliOptions->key);
  64. } else {
  65. error('You need to specify the key to delete.');
  66. exit;
  67. }
  68. break;
  69. case 'exist':
  70. if (($cliOptions->key ?? '') !== '') {
  71. $key = $cliOptions->key;
  72. if ($i18nData->isKnown($key)) {
  73. echo "The '{$key}' key is known.\n\n";
  74. } else {
  75. echo "The '{$key}' key is unknown.\n\n";
  76. }
  77. } else {
  78. error('You need to specify the key to check.');
  79. exit;
  80. }
  81. break;
  82. case 'format':
  83. break;
  84. case 'ignore':
  85. if (($cliOptions->language ?? '') !== '' && ($cliOptions->key ?? '') !== '') {
  86. $i18nData->ignore($cliOptions->key, $cliOptions->language, $cliOptions->revert);
  87. } else {
  88. error('You need to specify a valid set of options.');
  89. exit;
  90. }
  91. break;
  92. case 'ignore_unmodified':
  93. if (($cliOptions->language ?? '') !== '') {
  94. $i18nData->ignore_unmodified($cliOptions->language, $cliOptions->revert);
  95. } else {
  96. error('You need to specify a valid set of options.');
  97. exit;
  98. }
  99. break;
  100. default:
  101. manipulateHelp();
  102. exit;
  103. }
  104. $data->dump($i18nData->getData());
  105. /**
  106. * Output error message.
  107. */
  108. function error(string $message): void {
  109. $error = <<<ERROR
  110. WARNING
  111. %s\n\n
  112. ERROR;
  113. echo sprintf($error, $message);
  114. manipulateHelp();
  115. }
  116. /**
  117. * Output help message.
  118. */
  119. function manipulateHelp(): void {
  120. $file = str_replace(__DIR__ . '/', '', __FILE__);
  121. echo <<<HELP
  122. NAME
  123. $file
  124. SYNOPSIS
  125. php $file [OPTIONS]
  126. DESCRIPTION
  127. Manipulate translation files.
  128. -a, --action=ACTION
  129. select the action to perform. Available actions are add, move, delete,
  130. exist, format, ignore, and ignore_unmodified. This option is mandatory.
  131. -k, --key=KEY select the key to work on.
  132. -v, --value=VAL select the value to set.
  133. -l, --language=LANG select the language to work on.
  134. -h, --help display this help and exit.
  135. -r, --revert revert the action (only for ignore action)
  136. -o, origin-language=LANG
  137. select the origin language (only for add language action)
  138. EXAMPLES
  139. Example 1: add a language. Adds a new language by duplicating the reference language.
  140. php $file -a add -l my_lang
  141. php $file -a add -l my_lang -o ref_lang
  142. Example 2: add a new key. Adds a key to all supported languages.
  143. php $file -a add -k my_key -v my_value
  144. Example 3: add a new value. Sets a new value for the selected key in the selected language.
  145. php $file -a add -k my_key -v my_value -l my_lang
  146. Example 4: delete a key. Deletes the selected key from all supported languages.
  147. php $file -a delete -k my_key
  148. Example 5: format i18n files.
  149. php $file -a format
  150. Example 6: ignore a key. Adds IGNORE comment to the key in the selected language, marking it as translated.
  151. php $file -a ignore -k my_key -l my_lang
  152. Example 7: revert ignore a key. Removes IGNORE comment from the key in the selected language.
  153. php $file -a ignore -r -k my_key -l my_lang
  154. Example 8: ignore all unmodified keys. Adds IGNORE comments to all unmodified keys in the selected language, marking them as translated.
  155. php $file -a ignore_unmodified -l my_lang
  156. Example 9: revert ignore on all unmodified keys. Removes IGNORE comments from all unmodified keys in the selected language.
  157. Warning: will also revert individually added IGNORE(s) on unmodified keys.
  158. php $file -a ignore_unmodified -r -l my_lang
  159. Example 10: check if a key exist.
  160. php $file -a exist -k my_key
  161. Example 11: add a new file to all languages
  162. php $file -a add -k my_file.php
  163. Example 12:\tmove an existing key into a new location
  164. php $file -a move -k my_key -n new_location
  165. HELP, PHP_EOL;
  166. exit();
  167. }