manipulate.translation.php 5.3 KB

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