LocaleScanner.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Data\Util;
  11. /**
  12. * Scans a directory with data files for locales.
  13. *
  14. * The name of each file with the extension ".txt" is considered, if it "looks"
  15. * like a locale:
  16. *
  17. * - the name must start with two letters;
  18. * - the two letters may optionally be followed by an underscore and any
  19. * sequence of other symbols.
  20. *
  21. * For example, "de" and "de_DE" are considered to be locales. "root" and "meta"
  22. * are not.
  23. *
  24. * @author Bernhard Schussek <bschussek@gmail.com>
  25. *
  26. * @internal
  27. */
  28. class LocaleScanner
  29. {
  30. /**
  31. * Returns all locales found in the given directory.
  32. *
  33. * @return array An array of locales. The result also contains locales that
  34. * are in fact just aliases for other locales. Use
  35. * {@link scanAliases()} to determine which of the locales
  36. * are aliases
  37. */
  38. public function scanLocales(string $sourceDir): array
  39. {
  40. $locales = glob($sourceDir.'/*.txt', \GLOB_NOSORT);
  41. // Remove file extension and sort
  42. array_walk($locales, function (&$locale) { $locale = basename($locale, '.txt'); });
  43. // Remove non-locales
  44. $locales = array_filter($locales, function ($locale) {
  45. return preg_match('/^[a-z]{2}(_.+)?$/', $locale);
  46. });
  47. sort($locales);
  48. return $locales;
  49. }
  50. /**
  51. * Returns all locale aliases found in the given directory.
  52. *
  53. * @return array An array with the locale aliases as keys and the aliased
  54. * locales as values
  55. */
  56. public function scanAliases(string $sourceDir): array
  57. {
  58. $locales = $this->scanLocales($sourceDir);
  59. $aliases = [];
  60. // Delete locales that are no aliases
  61. foreach ($locales as $locale) {
  62. $content = file_get_contents($sourceDir.'/'.$locale.'.txt');
  63. // Aliases contain the text "%%ALIAS" followed by the aliased locale
  64. if (preg_match('/"%%ALIAS"\{"([^"]+)"\}/', $content, $matches)) {
  65. $aliases[$locale] = $matches[1];
  66. }
  67. }
  68. return $aliases;
  69. }
  70. /**
  71. * Returns all locale parents found in the given directory.
  72. */
  73. public function scanParents(string $sourceDir): array
  74. {
  75. $locales = $this->scanLocales($sourceDir);
  76. $fallbacks = [];
  77. foreach ($locales as $locale) {
  78. $content = file_get_contents($sourceDir.'/'.$locale.'.txt');
  79. // Aliases contain the text "%%PARENT" followed by the aliased locale
  80. if (preg_match('/%%Parent{"([^"]+)"}/', $content, $matches)) {
  81. $fallbacks[$locale] = $matches[1];
  82. }
  83. }
  84. return $fallbacks;
  85. }
  86. }