Exemplo n.º 1
0
 public function testScanAliases()
 {
     $sortedAliases = array('de_alias' => 'de', 'en_alias' => 'en', 'fr_alias' => 'fr');
     $this->assertSame($sortedAliases, $this->scanner->scanAliases($this->directory));
 }
 /**
  * {@inheritdoc}
  */
 public function generateData(GeneratorConfig $config)
 {
     $filesystem = new Filesystem();
     $localeScanner = new LocaleScanner();
     $writers = $config->getBundleWriters();
     // Prepare filesystem directories
     foreach ($writers as $targetDir => $writer) {
         $filesystem->remove($targetDir . '/' . $this->dirName);
         $filesystem->mkdir($targetDir . '/' . $this->dirName);
     }
     $locales = $localeScanner->scanLocales($config->getSourceDir() . '/locales');
     $aliases = $localeScanner->scanAliases($config->getSourceDir() . '/locales');
     // Flip to facilitate lookup
     $flippedLocales = array_flip($locales);
     // Don't generate names for aliases (names will be generated for the
     // locale they are duplicating)
     $displayLocales = array_diff_key($flippedLocales, $aliases);
     ksort($displayLocales);
     // Generate a list of (existing) locale fallbacks
     $fallbackMapping = $this->generateFallbackMapping($displayLocales, $aliases);
     $localeNames = array();
     // Generate locale names for all locales that have translations in
     // at least the language or the region bundle
     foreach ($displayLocales as $displayLocale => $_) {
         $localeNames[$displayLocale] = array();
         foreach ($locales as $locale) {
             try {
                 // Generate a locale name in the language of each display locale
                 // Each locale name has the form: "Language (Script, Region, Variant1, ...)
                 // Script, Region and Variants are optional. If none of them is
                 // available, the braces are not printed.
                 if (null !== ($name = $this->generateLocaleName($locale, $displayLocale))) {
                     $localeNames[$displayLocale][$locale] = $name;
                 }
             } catch (MissingResourceException $e) {
             } catch (ResourceBundleNotFoundException $e) {
             }
         }
     }
     // Process again to de-duplicate locales and their fallback locales
     // Only keep the differences
     foreach ($displayLocales as $displayLocale => $_) {
         $fallback = $displayLocale;
         while (isset($fallbackMapping[$fallback])) {
             $fallback = $fallbackMapping[$fallback];
             $localeNames[$displayLocale] = array_diff($localeNames[$displayLocale], $localeNames[$fallback]);
         }
         // If no names remain to be saved for the current locale, skip it
         if (0 === count($localeNames[$displayLocale])) {
             continue;
         }
         foreach ($writers as $targetDir => $writer) {
             $writer->write($targetDir . '/' . $this->dirName, $displayLocale, array('Names' => $localeNames[$displayLocale]));
         }
     }
     // Generate aliases, needed to enable proper fallback from alias to its
     // target
     foreach ($aliases as $alias => $aliasOf) {
         foreach ($writers as $targetDir => $writer) {
             $writer->write($targetDir . '/' . $this->dirName, $alias, array('%%ALIAS' => $aliasOf));
         }
     }
     // Create root file which maps locale codes to locale codes, for fallback
     foreach ($writers as $targetDir => $writer) {
         $writer->write($targetDir . '/' . $this->dirName, 'meta', array('Locales' => $locales, 'Aliases' => $aliases));
     }
 }