manipulate.translation.php 4.2 KB

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