manipulate.translation.php 2.6 KB

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