I18nFile.php 2.8 KB

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