I18nFile.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . '/I18nValue.php';
  4. class I18nFile {
  5. /**
  6. * @param array<mixed,mixed> $array
  7. * @phpstan-assert-if-true array<int|string,string|array<mixed>> $array
  8. */
  9. public static function is_array_recursive_string(array $array): bool {
  10. foreach ($array as $value) {
  11. if (!is_string($value) && !(is_array($value) && self::is_array_recursive_string($value))) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. /**
  18. * @return array<string,array<string,array<string,I18nValue>>>
  19. */
  20. public function load(): array {
  21. $i18n = [];
  22. $dirs = new DirectoryIterator(I18N_PATH);
  23. foreach ($dirs as $dir) {
  24. if ($dir->isDot()) {
  25. continue;
  26. }
  27. $files = new DirectoryIterator($dir->getPathname());
  28. foreach ($files as $file) {
  29. if (!$file->isFile()) {
  30. continue;
  31. }
  32. if ($file->getFilename() === 'plurals.php') {
  33. continue;
  34. }
  35. $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten($this->process($file->getPathname()), $file->getBasename('.php'));
  36. }
  37. }
  38. return $i18n;
  39. }
  40. /**
  41. * @param array<string,array<string,array<string,I18nValue>>> $i18n
  42. */
  43. public function dump(array $i18n): void {
  44. foreach ($i18n as $language => $file) {
  45. $dir = I18N_PATH . DIRECTORY_SEPARATOR . $language;
  46. if (!file_exists($dir)) {
  47. mkdir($dir, 0770, true);
  48. }
  49. foreach ($file as $name => $content) {
  50. $filename = $dir . DIRECTORY_SEPARATOR . $name;
  51. file_put_contents($filename, $this->format($content));
  52. }
  53. }
  54. }
  55. /**
  56. * Process the content of an i18n file
  57. * @return array<int|string,string|array<mixed>>
  58. */
  59. private function process(string $filename): array {
  60. $fileContent = file_get_contents($filename);
  61. if (!is_string($fileContent)) {
  62. return [];
  63. }
  64. $content = str_replace('<?php', '', $fileContent);
  65. $content = preg_replace([
  66. "#',\s*//\s*TODO.*#i",
  67. "#',\s*//\s*DIRTY.*#i",
  68. "#',\s*//\s*IGNORE.*#i",
  69. ], [
  70. ' -> todo\',',
  71. ' -> dirty\',',
  72. ' -> ignore\',',
  73. ], $content);
  74. try {
  75. $content = eval($content);
  76. } catch (ParseError $ex) {
  77. if (defined('STDERR')) {
  78. fwrite(STDERR, "Error while processing: $filename\n");
  79. fwrite(STDERR, $ex->getMessage());
  80. }
  81. die(1);
  82. }
  83. if (is_array($content) && self::is_array_recursive_string($content)) {
  84. return $content;
  85. }
  86. return [];
  87. }
  88. /**
  89. * Flatten an array of translation
  90. *
  91. * @param array<int|string,I18nValue|string|array<mixed>|mixed> $translation
  92. * @return array<string,I18nValue>
  93. */
  94. private function flatten(array $translation, string $prefix = ''): array {
  95. $a = [];
  96. if ('' !== $prefix) {
  97. $prefix .= '.';
  98. }
  99. foreach ($translation as $key => $value) {
  100. $key = (string)$key;
  101. if (is_array($value) && self::is_array_recursive_string($value)) {
  102. $a += $this->flatten($value, $prefix . $key);
  103. } elseif (is_string($value) || $value instanceof I18nValue) {
  104. $a[$prefix . $key] = new I18nValue($value);
  105. }
  106. }
  107. return $a;
  108. }
  109. /**
  110. * Unflatten an array of translation
  111. *
  112. * The first key is dropped since it represents the filename and we have
  113. * no use of it.
  114. *
  115. * @param array<string,I18nValue> $translation
  116. * @return array<int|string,mixed>
  117. */
  118. private function unflatten(array $translation): array {
  119. $a = [];
  120. ksort($translation, SORT_NATURAL);
  121. foreach ($translation as $compoundKey => $value) {
  122. $keys = explode('.', $compoundKey);
  123. array_shift($keys);
  124. $current =& $a;
  125. $lastIndex = count($keys) - 1;
  126. foreach ($keys as $index => $key) {
  127. $normalisedKey = ctype_digit($key) ? (int)$key : $key;
  128. if ($index === $lastIndex) {
  129. $current[$normalisedKey] = $value->__toString();
  130. continue;
  131. }
  132. if (!isset($current[$normalisedKey]) || !is_array($current[$normalisedKey])) {
  133. $current[$normalisedKey] = [];
  134. }
  135. $current =& $current[$normalisedKey];
  136. }
  137. unset($current);
  138. }
  139. return $a;
  140. }
  141. /**
  142. * Format an array of translation
  143. *
  144. * It takes an array of translation and format it to be dumped in a
  145. * translation file. The array is first converted to a string then some
  146. * formatting regexes are applied to match the original content.
  147. *
  148. * @param array<string,I18nValue> $translation
  149. */
  150. private function format(array $translation): string {
  151. $translation = var_export($this->unflatten($translation), true);
  152. $patterns = [
  153. '/ -> todo\',/',
  154. '/ -> dirty\',/',
  155. '/ -> ignore\',/',
  156. '/array \(/',
  157. '/=>\s*array/',
  158. '/(\w) {2}/',
  159. '/ {2}/',
  160. ];
  161. $replacements = [
  162. "',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string
  163. "',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string
  164. "',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string
  165. 'array(',
  166. '=> array',
  167. '$1 ',
  168. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  169. ];
  170. $translation = preg_replace($patterns, $replacements, $translation);
  171. return <<<PHP
  172. <?php
  173. /******************************************************************************
  174. * Each entry of that file can be associated with a comment to indicate its *
  175. * state. When there is no comment, it means the entry is fully translated. *
  176. * The recognized comments are (comment matching is case-insensitive): *
  177. * + TODO: the entry has never been translated. *
  178. * + DIRTY: the entry has been translated but needs to be updated. *
  179. * + IGNORE: the entry does not need to be translated. *
  180. * When a comment is not recognized, it is discarded. *
  181. ******************************************************************************/
  182. return {$translation};
  183. PHP;
  184. }
  185. }