I18nFile.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. require_once __DIR__ . '/I18nData.php';
  3. class i18nFile {
  4. private $i18nPath;
  5. public function __construct() {
  6. $this->i18nPath = __DIR__ . '/../../app/i18n';
  7. }
  8. public function load() {
  9. $dirs = new DirectoryIterator($this->i18nPath);
  10. foreach ($dirs as $dir) {
  11. if ($dir->isDot()) {
  12. continue;
  13. }
  14. $files = new DirectoryIterator($dir->getPathname());
  15. foreach ($files as $file) {
  16. if (!$file->isFile()) {
  17. continue;
  18. }
  19. $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten(include $file->getPathname(), $file->getBasename('.php'));
  20. }
  21. }
  22. return new I18nData($i18n);
  23. }
  24. public function dump(I18nData $i18n) {
  25. foreach ($i18n->getData() as $language => $file) {
  26. $dir = $this->i18nPath . DIRECTORY_SEPARATOR . $language;
  27. if (!file_exists($dir)) {
  28. mkdir($dir);
  29. }
  30. foreach ($file as $name => $content) {
  31. $filename = $dir . DIRECTORY_SEPARATOR . $name;
  32. file_put_contents($filename, $this->format($content));
  33. }
  34. }
  35. }
  36. /**
  37. * Flatten an array of translation
  38. *
  39. * @param array $translation
  40. * @param string $prefix
  41. * @return array
  42. */
  43. private function flatten($translation, $prefix = '') {
  44. $a = array();
  45. if ('' !== $prefix) {
  46. $prefix .= '.';
  47. }
  48. foreach ($translation as $key => $value) {
  49. if (is_array($value)) {
  50. $a += $this->flatten($value, $prefix . $key);
  51. } else {
  52. $a[$prefix . $key] = $value;
  53. }
  54. }
  55. return $a;
  56. }
  57. /**
  58. * Unflatten an array of translation
  59. *
  60. * The first key is dropped since it represents the filename and we have
  61. * no use of it.
  62. *
  63. * @param array $translation
  64. * @return array
  65. */
  66. private function unflatten($translation) {
  67. $a = array();
  68. ksort($translation);
  69. foreach ($translation as $compoundKey => $value) {
  70. $keys = explode('.', $compoundKey);
  71. array_shift($keys);
  72. eval("\$a['" . implode("']['", $keys) . "'] = '" . $value . "';");
  73. }
  74. return $a;
  75. }
  76. /**
  77. * Format an array of translation
  78. *
  79. * It takes an array of translation and format it to be dumped in a
  80. * translation file. The array is first converted to a string then some
  81. * formatting regexes are applied to match the original content.
  82. *
  83. * @param array $translation
  84. * @return string
  85. */
  86. private function format($translation) {
  87. $translation = var_export($this->unflatten($translation), true);
  88. $patterns = array(
  89. '/array \(/',
  90. '/=>\s*array/',
  91. '/ {2}/',
  92. );
  93. $replacements = array(
  94. 'array(',
  95. '=> array',
  96. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  97. );
  98. $translation = preg_replace($patterns, $replacements, $translation);
  99. // Double quoting is mandatory to have new lines instead of \n strings
  100. return sprintf("<?php\n\nreturn %s;\n", $translation);
  101. }
  102. }