Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function generateData(GeneratorConfig $config)
 {
     $filesystem = new Filesystem();
     $localeScanner = new LocaleScanner();
     $reader = new IntlBundleReader();
     $writers = $config->getBundleWriters();
     $tempDir = sys_get_temp_dir() . '/icu-data-' . $this->dirName;
     // Prepare filesystem directories
     foreach ($writers as $targetDir => $writer) {
         $filesystem->remove($targetDir . '/' . $this->dirName);
         $filesystem->mkdir($targetDir . '/' . $this->dirName);
     }
     $filesystem->remove($tempDir);
     $filesystem->mkdir($tempDir);
     $locales = $this->scanLocales($localeScanner, $config->getSourceDir());
     $this->compileTemporaryBundles($this->compiler, $config->getSourceDir(), $tempDir);
     $this->preGenerate();
     foreach ($locales as $locale) {
         $localeData = $this->generateDataForLocale($reader, $tempDir, $locale);
         if (null !== $localeData) {
             foreach ($writers as $targetDir => $writer) {
                 $writer->write($targetDir . '/' . $this->dirName, $locale, $localeData);
             }
         }
     }
     $rootData = $this->generateDataForRoot($reader, $tempDir);
     if (null !== $rootData) {
         foreach ($writers as $targetDir => $writer) {
             $writer->write($targetDir . '/' . $this->dirName, 'root', $rootData);
         }
     }
     $metaData = $this->generateDataForMeta($reader, $tempDir);
     if (null !== $metaData) {
         foreach ($writers as $targetDir => $writer) {
             $writer->write($targetDir . '/' . $this->dirName, 'meta', $metaData);
         }
     }
     // Clean up
     $filesystem->remove($tempDir);
 }
 /**
  * {@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));
     }
 }