I18nFile.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. require_once __DIR__ . '/I18nValue.php';
  3. class I18nFile {
  4. private $i18nPath;
  5. public function __construct() {
  6. $this->i18nPath = __DIR__ . '/../../app/i18n';
  7. }
  8. public function load() {
  9. $i18n = array();
  10. $dirs = new DirectoryIterator($this->i18nPath);
  11. foreach ($dirs as $dir) {
  12. if ($dir->isDot()) {
  13. continue;
  14. }
  15. $files = new DirectoryIterator($dir->getPathname());
  16. foreach ($files as $file) {
  17. if (!$file->isFile()) {
  18. continue;
  19. }
  20. $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten($this->process($file->getPathname()), $file->getBasename('.php'));
  21. }
  22. }
  23. return $i18n;
  24. }
  25. public function dump(array $i18n) {
  26. foreach ($i18n as $language => $file) {
  27. $dir = $this->i18nPath . DIRECTORY_SEPARATOR . $language;
  28. if (!file_exists($dir)) {
  29. mkdir($dir);
  30. }
  31. foreach ($file as $name => $content) {
  32. $filename = $dir . DIRECTORY_SEPARATOR . $name;
  33. file_put_contents($filename, $this->format($content));
  34. }
  35. }
  36. }
  37. /**
  38. * Process the content of an i18n file
  39. *
  40. * @param string $filename
  41. * @return array
  42. */
  43. private function process(string $filename) {
  44. $content = file_get_contents($filename);
  45. $content = str_replace('<?php', '', $content);
  46. $content = preg_replace([
  47. "#',\s*//\s*TODO#i",
  48. "#',\s*//\s*DIRTY#i",
  49. "#',\s*//\s*IGNORE#i",
  50. ], [
  51. ' -> todo\',',
  52. ' -> dirty\',',
  53. ' -> ignore\',',
  54. ], $content);
  55. $content = eval($content);
  56. if (is_array($content)) {
  57. return $content;
  58. }
  59. return [];
  60. }
  61. /**
  62. * Flatten an array of translation
  63. *
  64. * @param array $translation
  65. * @param string $prefix
  66. * @return array
  67. */
  68. private function flatten(array $translation, string $prefix = '') {
  69. $a = array();
  70. if ('' !== $prefix) {
  71. $prefix .= '.';
  72. }
  73. foreach ($translation as $key => $value) {
  74. if (is_array($value)) {
  75. $a += $this->flatten($value, $prefix . $key);
  76. } else {
  77. $a[$prefix . $key] = new I18nValue($value);
  78. }
  79. }
  80. return $a;
  81. }
  82. /**
  83. * Unflatten an array of translation
  84. *
  85. * The first key is dropped since it represents the filename and we have
  86. * no use of it.
  87. *
  88. * @param array $translation
  89. * @return array
  90. */
  91. private function unflatten(array $translation) {
  92. $a = array();
  93. ksort($translation, SORT_NATURAL);
  94. foreach ($translation as $compoundKey => $value) {
  95. $keys = explode('.', $compoundKey);
  96. array_shift($keys);
  97. eval("\$a['" . implode("']['", $keys) . "'] = '" . addcslashes($value, "'") . "';");
  98. }
  99. return $a;
  100. }
  101. /**
  102. * Format an array of translation
  103. *
  104. * It takes an array of translation and format it to be dumped in a
  105. * translation file. The array is first converted to a string then some
  106. * formatting regexes are applied to match the original content.
  107. *
  108. * @param array $translation
  109. * @return string
  110. */
  111. private function format(array $translation) {
  112. $translation = var_export($this->unflatten($translation), true);
  113. $patterns = array(
  114. '/ -> todo\',/',
  115. '/ -> dirty\',/',
  116. '/ -> ignore\',/',
  117. '/array \(/',
  118. '/=>\s*array/',
  119. '/(\w) {2}/',
  120. '/ {2}/',
  121. );
  122. $replacements = array(
  123. "',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string
  124. "',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string
  125. "',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string
  126. 'array(',
  127. '=> array',
  128. '$1 ',
  129. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  130. );
  131. $translation = preg_replace($patterns, $replacements, $translation);
  132. return <<<OUTPUT
  133. <?php
  134. /******************************************************************************/
  135. /* Each entry of that file can be associated with a comment to indicate its */
  136. /* state. When there is no comment, it means the entry is fully translated. */
  137. /* The recognized comments are (comment matching is case-insensitive): */
  138. /* + TODO: the entry has never been translated. */
  139. /* + DIRTY: the entry has been translated but needs to be updated. */
  140. /* + IGNORE: the entry does not need to be translated. */
  141. /* When a comment is not recognized, it is discarded. */
  142. /******************************************************************************/
  143. return {$translation};
  144. OUTPUT;
  145. }
  146. }