manipulate.translation.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. $options = getopt("a:hk:l:rv:");
  3. if (array_key_exists('h', $options)) {
  4. help();
  5. }
  6. if (!array_key_exists('a', $options)) {
  7. error('You need to specify the action to perform.');
  8. }
  9. if ('ignore' === $options['a']) {
  10. require_once __DIR__ . '/i18n/I18nIgnoreFile.php';
  11. $i18nFile = new I18nIgnoreFile();
  12. } else {
  13. require_once __DIR__ . '/i18n/I18nFile.php';
  14. $i18nFile = new I18nFile();
  15. }
  16. $i18nData = $i18nFile->load();
  17. switch ($options['a']) {
  18. case 'add' :
  19. if (array_key_exists('k', $options) && array_key_exists('v', $options) && array_key_exists('l', $options)) {
  20. $i18nData->addValue($options['k'], $options['v'], $options['l']);
  21. } elseif (array_key_exists('k', $options) && array_key_exists('v', $options)) {
  22. $i18nData->addKey($options['k'], $options['v']);
  23. } elseif (array_key_exists('l', $options)) {
  24. $i18nData->addLanguage($options['l']);
  25. } else {
  26. error('You need to specify a valid set of options.');
  27. }
  28. break;
  29. case 'delete' :
  30. if (array_key_exists('k', $options)) {
  31. $i18nData->removeKey($options['k']);
  32. } else {
  33. error('You need to specify the key to delete.');
  34. }
  35. break;
  36. case 'duplicate' :
  37. if (array_key_exists('k', $options)) {
  38. $i18nData->duplicateKey($options['k']);
  39. } else {
  40. error('You need to specify the key to duplicate');
  41. }
  42. break;
  43. case 'format' :
  44. $i18nFile->dump($i18nData);
  45. break;
  46. case 'ignore' :
  47. if (array_key_exists('l', $options) && array_key_exists('k', $options)) {
  48. $i18nData->ignore($options['k'], $options['l'], array_key_exists('r', $options));
  49. } else {
  50. error('You need to specify a valid set of options.');
  51. }
  52. break;
  53. default :
  54. help();
  55. }
  56. if ($i18nData->hasChanged()) {
  57. $i18nFile->dump($i18nData);
  58. }
  59. /**
  60. * Output error message.
  61. */
  62. function error($message) {
  63. $error = <<<ERROR
  64. WARNING
  65. %s\n\n
  66. ERROR;
  67. echo sprintf($error, $message);
  68. help();
  69. }
  70. /**
  71. * Output help message.
  72. */
  73. function help() {
  74. $help = <<<HELP
  75. NAME
  76. %1\$s
  77. SYNOPSIS
  78. php %1\$s [OPTIONS]
  79. DESCRIPTION
  80. Manipulate translation files.
  81. -a=ACTION
  82. select the action to perform. Available actions are add, delete,
  83. duplicate, format, and ignore. This option is mandatory.
  84. -k=KEY select the key to work on.
  85. -v=VAL select the value to set.
  86. -l=LANG select the language to work on.
  87. -h display this help and exit.
  88. EXEMPLE
  89. Exemple 1: add a language. It adds a new language by duplicating the referential.
  90. php %1\$s -a add -l my_lang
  91. Exemple 2: add a new key. It adds the key in the referential.
  92. php %1\$s -a add -k my_key -v my_value
  93. Exemple 3: add a new value. It adds a new value for the selected key in the selected language.
  94. php %1\$s -a add -k my_key -v my_value -l my_lang
  95. Exemple 4: delete a key. It deletes the selected key in every languages.
  96. php %1\$s -a delete -k my_key
  97. Exemple 5: duplicate a key. It duplicates the key from the referential in every languages.
  98. php %1\$s -a duplicate -k my_key
  99. Exemple 6: format i18n files.
  100. php %1\$s -a format
  101. Exemple 7: ignore a key. It adds the key in the ignore file to mark it as translated.
  102. php %1\$s -a ignore -k my_key -l my_lang
  103. Exemple 8: revert ignore a key. It removes the key from the ignore file.
  104. php %1\$s -a ignore -r -k my_key -l my_lang\n\n
  105. HELP;
  106. $file = str_replace(__DIR__ . '/', '', __FILE__);
  107. echo sprintf($help, $file);
  108. exit;
  109. }