I18nFile.php 4.7 KB

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