I18nIgnoreFile.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. require_once __DIR__ . '/I18nFileInterface.php';
  3. class I18nIgnoreFile implements I18nFileInterface {
  4. private $i18nPath;
  5. public function __construct() {
  6. $this->i18nPath = __DIR__ . '/ignore';
  7. }
  8. public function dump(array $i18n) {
  9. foreach ($i18n as $language => $content) {
  10. $filename = $this->i18nPath . DIRECTORY_SEPARATOR . $language . '.php';
  11. file_put_contents($filename, $this->format($content));
  12. }
  13. }
  14. public function load() {
  15. $i18n = array();
  16. $files = new DirectoryIterator($this->i18nPath);
  17. foreach ($files as $file) {
  18. if (!$file->isFile()) {
  19. continue;
  20. }
  21. $i18n[$file->getBasename('.php')] = (include $file->getPathname());
  22. }
  23. return $i18n;
  24. }
  25. /**
  26. * Format an array of translation
  27. *
  28. * It takes an array of translation and format it to be dumped in a
  29. * translation file. The array is first converted to a string then some
  30. * formatting regexes are applied to match the original content.
  31. *
  32. * @param array $translation
  33. * @return string
  34. */
  35. private function format($translation) {
  36. $translation = var_export(($translation), true);
  37. $patterns = array(
  38. '/array \(/',
  39. '/=>\s*array/',
  40. '/ {2}/',
  41. '/\d+ => /',
  42. );
  43. $replacements = array(
  44. 'array(',
  45. '=> array',
  46. "\t", // Double quoting is mandatory to have a tab instead of the \t string
  47. '',
  48. );
  49. $translation = preg_replace($patterns, $replacements, $translation);
  50. // Double quoting is mandatory to have new lines instead of \n strings
  51. return sprintf("<?php\n\nreturn %s;\n", $translation);
  52. }
  53. }