I18nFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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<string,string|array<string,mixed>> $array
  8. */
  9. public static function is_array_recursive_string(array $array): bool {
  10. foreach ($array as $key => $value) {
  11. if (!is_string($key)) {
  12. return false;
  13. }
  14. if (!is_string($value) && !(is_array($value) && self::is_array_recursive_string($value))) {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20. /**
  21. * @return array<string,array<string,array<string,I18nValue>>>
  22. */
  23. public function load(): array {
  24. $i18n = [];
  25. $dirs = new DirectoryIterator(I18N_PATH);
  26. foreach ($dirs as $dir) {
  27. if ($dir->isDot()) {
  28. continue;
  29. }
  30. $files = new DirectoryIterator($dir->getPathname());
  31. foreach ($files as $file) {
  32. if (!$file->isFile()) {
  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<string,string|array<string,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<string,I18nValue|string|array<string,I18nValue>|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. if (is_array($value) && is_array_keys_string($value)) {
  101. $a += $this->flatten($value, $prefix . $key);
  102. } elseif (is_string($value) || $value instanceof I18nValue) {
  103. $a[$prefix . $key] = new I18nValue($value);
  104. }
  105. }
  106. return $a;
  107. }
  108. /**
  109. * Unflatten an array of translation
  110. *
  111. * The first key is dropped since it represents the filename and we have
  112. * no use of it.
  113. *
  114. * @param array<string,I18nValue> $translation
  115. * @return array<string,array<string,I18nValue>>
  116. */
  117. private function unflatten(array $translation): array {
  118. $a = [];
  119. ksort($translation, SORT_NATURAL);
  120. foreach ($translation as $compoundKey => $value) {
  121. $keys = explode('.', $compoundKey);
  122. array_shift($keys);
  123. eval("\$a['" . implode("']['", $keys) . "'] = '" . addcslashes($value->__toString(), "'") . "';");
  124. }
  125. return $a;
  126. }
  127. /**
  128. * Format an array of translation
  129. *
  130. * It takes an array of translation and format it to be dumped in a
  131. * translation file. The array is first converted to a string then some
  132. * formatting regexes are applied to match the original content.
  133. *
  134. * @param array<string,I18nValue> $translation
  135. */
  136. private function format(array $translation): string {
  137. $translation = var_export($this->unflatten($translation), true);
  138. $patterns = [
  139. '/ -> todo\',/',
  140. '/ -> dirty\',/',
  141. '/ -> ignore\',/',
  142. '/array \(/',
  143. '/=>\s*array/',
  144. '/(\w) {2}/',
  145. '/ {2}/',
  146. ];
  147. $replacements = [
  148. "',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string
  149. "',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string
  150. "',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string
  151. 'array(',
  152. '=> array',
  153. '$1 ',
  154. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  155. ];
  156. $translation = preg_replace($patterns, $replacements, $translation);
  157. return <<<PHP
  158. <?php
  159. /******************************************************************************
  160. * Each entry of that file can be associated with a comment to indicate its *
  161. * state. When there is no comment, it means the entry is fully translated. *
  162. * The recognized comments are (comment matching is case-insensitive): *
  163. * + TODO: the entry has never been translated. *
  164. * + DIRTY: the entry has been translated but needs to be updated. *
  165. * + IGNORE: the entry does not need to be translated. *
  166. * When a comment is not recognized, it is discarded. *
  167. ******************************************************************************/
  168. return {$translation};
  169. PHP;
  170. }
  171. }