manipulate.translation.php 5.2 KB

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