manipulate.translation.php 4.2 KB

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