manipulate.translation.php 4.1 KB

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