manipulate.translation.php 5.1 KB

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