manipulate.translation.php 4.2 KB

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