I18nFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $content = str_replace('<?php', '', $fileContent);
  62. $content = preg_replace([
  63. "#',\s*//\s*TODO.*#i",
  64. "#',\s*//\s*DIRTY.*#i",
  65. "#',\s*//\s*IGNORE.*#i",
  66. ], [
  67. ' -> todo\',',
  68. ' -> dirty\',',
  69. ' -> ignore\',',
  70. ], $content);
  71. try {
  72. $content = eval($content);
  73. } catch (ParseError $ex) {
  74. if (defined('STDERR')) {
  75. fwrite(STDERR, "Error while processing: $filename\n");
  76. fwrite(STDERR, $ex->getMessage());
  77. }
  78. die(1);
  79. }
  80. if (is_array($content) && self::is_array_recursive_string($content)) {
  81. return $content;
  82. }
  83. return [];
  84. }
  85. /**
  86. * Flatten an array of translation
  87. *
  88. * @param array<string,I18nValue|string|array<string,I18nValue>|mixed> $translation
  89. * @return array<string,I18nValue>
  90. */
  91. private function flatten(array $translation, string $prefix = ''): array {
  92. $a = [];
  93. if ('' !== $prefix) {
  94. $prefix .= '.';
  95. }
  96. foreach ($translation as $key => $value) {
  97. if (is_array($value) && is_array_keys_string($value)) {
  98. $a += $this->flatten($value, $prefix . $key);
  99. } elseif (is_string($value) || $value instanceof I18nValue) {
  100. $a[$prefix . $key] = new I18nValue($value);
  101. }
  102. }
  103. return $a;
  104. }
  105. /**
  106. * Unflatten an array of translation
  107. *
  108. * The first key is dropped since it represents the filename and we have
  109. * no use of it.
  110. *
  111. * @param array<string,I18nValue> $translation
  112. * @return array<string,array<string,I18nValue>>
  113. */
  114. private function unflatten(array $translation): array {
  115. $a = [];
  116. ksort($translation, SORT_NATURAL);
  117. foreach ($translation as $compoundKey => $value) {
  118. $keys = explode('.', $compoundKey);
  119. array_shift($keys);
  120. eval("\$a['" . implode("']['", $keys) . "'] = '" . addcslashes($value->__toString(), "'") . "';");
  121. }
  122. return $a;
  123. }
  124. /**
  125. * Format an array of translation
  126. *
  127. * It takes an array of translation and format it to be dumped in a
  128. * translation file. The array is first converted to a string then some
  129. * formatting regexes are applied to match the original content.
  130. *
  131. * @param array<string,I18nValue> $translation
  132. */
  133. private function format(array $translation): string {
  134. $translation = var_export($this->unflatten($translation), true);
  135. $patterns = [
  136. '/ -> todo\',/',
  137. '/ -> dirty\',/',
  138. '/ -> ignore\',/',
  139. '/array \(/',
  140. '/=>\s*array/',
  141. '/(\w) {2}/',
  142. '/ {2}/',
  143. ];
  144. $replacements = [
  145. "',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string
  146. "',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string
  147. "',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string
  148. 'array(',
  149. '=> array',
  150. '$1 ',
  151. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  152. ];
  153. $translation = preg_replace($patterns, $replacements, $translation);
  154. return <<<OUTPUT
  155. <?php
  156. /******************************************************************************/
  157. /* Each entry of that file can be associated with a comment to indicate its */
  158. /* state. When there is no comment, it means the entry is fully translated. */
  159. /* The recognized comments are (comment matching is case-insensitive): */
  160. /* + TODO: the entry has never been translated. */
  161. /* + DIRTY: the entry has been translated but needs to be updated. */
  162. /* + IGNORE: the entry does not need to be translated. */
  163. /* When a comment is not recognized, it is discarded. */
  164. /******************************************************************************/
  165. return {$translation};
  166. OUTPUT;
  167. }
  168. }