manipulate.translation.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: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. $i18nData->addLanguage($options['l']);
  23. } else {
  24. error('You need to specify a valid set of options.');
  25. exit;
  26. }
  27. break;
  28. case 'delete' :
  29. if (array_key_exists('k', $options)) {
  30. $i18nData->removeKey($options['k']);
  31. } else {
  32. error('You need to specify the key to delete.');
  33. exit;
  34. }
  35. break;
  36. case 'format' :
  37. break;
  38. case 'ignore' :
  39. if (array_key_exists('l', $options) && array_key_exists('k', $options)) {
  40. $i18nData->ignore($options['k'], $options['l'], array_key_exists('r', $options));
  41. } else {
  42. error('You need to specify a valid set of options.');
  43. exit;
  44. }
  45. break;
  46. default :
  47. help();
  48. exit;
  49. }
  50. $data->dump($i18nData->getData());
  51. $ignore->dump($i18nData->getIgnore());
  52. /**
  53. * Output error message.
  54. */
  55. function error($message) {
  56. $error = <<<ERROR
  57. WARNING
  58. %s\n\n
  59. ERROR;
  60. echo sprintf($error, $message);
  61. help();
  62. }
  63. /**
  64. * Output help message.
  65. */
  66. function help() {
  67. $help = <<<HELP
  68. NAME
  69. %1\$s
  70. SYNOPSIS
  71. php %1\$s [OPTIONS]
  72. DESCRIPTION
  73. Manipulate translation files.
  74. -a=ACTION
  75. select the action to perform. Available actions are add, delete,
  76. format, and ignore. This option is mandatory.
  77. -k=KEY select the key to work on.
  78. -v=VAL select the value to set.
  79. -l=LANG select the language to work on.
  80. -h display this help and exit.
  81. EXEMPLE
  82. Exemple 1: add a language. It adds a new language by duplicating the referential.
  83. php %1\$s -a add -l my_lang
  84. Exemple 2: add a new key. It adds the key for all supported languages.
  85. php %1\$s -a add -k my_key -v my_value
  86. Exemple 3: add a new value. It adds a new value for the selected key in the selected language.
  87. php %1\$s -a add -k my_key -v my_value -l my_lang
  88. Exemple 4: delete a key. It deletes the selected key from all supported languages.
  89. php %1\$s -a delete -k my_key
  90. Exemple 5: format i18n files.
  91. php %1\$s -a format
  92. Exemple 6: ignore a key. It adds the key in the ignore file to mark it as translated.
  93. php %1\$s -a ignore -k my_key -l my_lang
  94. Exemple 7: revert ignore a key. It removes the key from the ignore file.
  95. php %1\$s -a ignore -r -k my_key -l my_lang\n\n
  96. HELP;
  97. $file = str_replace(__DIR__ . '/', '', __FILE__);
  98. echo sprintf($help, $file);
  99. exit;
  100. }