manipulate.translation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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__ . '/i18n/I18nIgnoreFile.php';
  6. $options = getopt("a:hk:l:o:rv:");
  7. if (array_key_exists('h', $options)) {
  8. help();
  9. }
  10. if (!array_key_exists('a', $options)) {
  11. error('You need to specify the action to perform.');
  12. }
  13. $data = new I18nFile();
  14. $ignore = new I18nIgnoreFile();
  15. $i18nData = new I18nData($data->load(), $ignore->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. help();
  74. exit;
  75. }
  76. $data->dump($i18nData->getData());
  77. $ignore->dump($i18nData->getIgnore());
  78. /**
  79. * Output error message.
  80. */
  81. function error($message) {
  82. $error = <<<ERROR
  83. WARNING
  84. %s\n\n
  85. ERROR;
  86. echo sprintf($error, $message);
  87. help();
  88. }
  89. /**
  90. * Output help message.
  91. */
  92. function help() {
  93. $help = <<<HELP
  94. NAME
  95. %1\$s
  96. SYNOPSIS
  97. php %1\$s [OPTIONS]
  98. DESCRIPTION
  99. Manipulate translation files.
  100. -a=ACTION
  101. select the action to perform. Available actions are add, delete,
  102. exist, format, and ignore. 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. EXEMPLE
  110. Exemple 1: add a language. It adds a new language by duplicating the referential.
  111. php %1\$s -a add -l my_lang
  112. php %1\$s -a add -l my_lang -o ref_lang
  113. Exemple 2: add a new key. It adds the key for all supported languages.
  114. php %1\$s -a add -k my_key -v my_value
  115. Exemple 3: add a new value. It adds a new value for the selected key in the selected language.
  116. php %1\$s -a add -k my_key -v my_value -l my_lang
  117. Exemple 4: delete a key. It deletes the selected key from all supported languages.
  118. php %1\$s -a delete -k my_key
  119. Exemple 5: format i18n files.
  120. php %1\$s -a format
  121. Exemple 6: ignore a key. It adds the key in the ignore file to mark it as translated.
  122. php %1\$s -a ignore -k my_key -l my_lang
  123. Exemple 7: revert ignore a key. It removes the key from the ignore file.
  124. php %1\$s -a ignore -r -k my_key -l my_lang
  125. Exemple 8: ignore all unmodified keys. It adds all modified keys in the ignore file to mark it as translated.
  126. php %1\$s -a ignore_unmodified -l my_lang
  127. 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.
  128. php %1\$s -a ignore_unmodified -r -l my_lang
  129. Exemple 10: check if a key exist.
  130. php %1\$s -a exist -k my_key\n\n
  131. HELP;
  132. $file = str_replace(__DIR__ . '/', '', __FILE__);
  133. echo sprintf($help, $file);
  134. exit;
  135. }