manipulate.translation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. default :
  64. help();
  65. exit;
  66. }
  67. $data->dump($i18nData->getData());
  68. $ignore->dump($i18nData->getIgnore());
  69. /**
  70. * Output error message.
  71. */
  72. function error($message) {
  73. $error = <<<ERROR
  74. WARNING
  75. %s\n\n
  76. ERROR;
  77. echo sprintf($error, $message);
  78. help();
  79. }
  80. /**
  81. * Output help message.
  82. */
  83. function help() {
  84. $help = <<<HELP
  85. NAME
  86. %1\$s
  87. SYNOPSIS
  88. php %1\$s [OPTIONS]
  89. DESCRIPTION
  90. Manipulate translation files.
  91. -a=ACTION
  92. select the action to perform. Available actions are add, delete,
  93. exist, format, and ignore. This option is mandatory.
  94. -k=KEY select the key to work on.
  95. -v=VAL select the value to set.
  96. -l=LANG select the language to work on.
  97. -h display this help and exit.
  98. -r revert the action (only for ignore action)
  99. -o=LANG select the origin language (only for add language action)
  100. EXEMPLE
  101. Exemple 1: add a language. It adds a new language by duplicating the referential.
  102. php %1\$s -a add -l my_lang
  103. php %1\$s -a add -l my_lang -o ref_lang
  104. Exemple 2: add a new key. It adds the key for all supported languages.
  105. php %1\$s -a add -k my_key -v my_value
  106. Exemple 3: add a new value. It adds a new value for the selected key in the selected language.
  107. php %1\$s -a add -k my_key -v my_value -l my_lang
  108. Exemple 4: delete a key. It deletes the selected key from all supported languages.
  109. php %1\$s -a delete -k my_key
  110. Exemple 5: format i18n files.
  111. php %1\$s -a format
  112. Exemple 6: ignore a key. It adds the key in the ignore file to mark it as translated.
  113. php %1\$s -a ignore -k my_key -l my_lang
  114. Exemple 7: revert ignore a key. It removes the key from the ignore file.
  115. php %1\$s -a ignore -r -k my_key -l my_lang
  116. Exemple 8: check if a key exist.
  117. php %1\$s -a exist -k my_key\n\n
  118. HELP;
  119. $file = str_replace(__DIR__ . '/', '', __FILE__);
  120. echo sprintf($help, $file);
  121. exit;
  122. }