manipulate.translation.php 4.2 KB

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