manipulate.translation.php 5.7 KB

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