I18nFile.php 2.9 KB

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