Neos\Flow\I18n\Service::getLocaleChain PHP Метод

getLocaleChain() публичный Метод

Build a chain of locale objects according to the fallback rule and the available locales.
public getLocaleChain ( Locale $locale ) : array
$locale Locale
Результат array
    public function getLocaleChain(Locale $locale)
    {
        $fallbackRule = $this->configuration->getFallbackRule();
        $localeChain = [(string) $locale => $locale];
        if ($fallbackRule['strict'] === true) {
            foreach ($fallbackRule['order'] as $localeIdentifier) {
                $localeChain[$localeIdentifier] = new Locale($localeIdentifier);
            }
        } else {
            $locale = $this->findBestMatchingLocale($locale);
            while ($locale !== null) {
                $localeChain[(string) $locale] = $locale;
                $locale = $this->getParentLocaleOf($locale);
            }
            foreach ($fallbackRule['order'] as $localeIdentifier) {
                $locale = new Locale($localeIdentifier);
                $locale = $this->findBestMatchingLocale($locale);
                while ($locale !== null) {
                    $localeChain[(string) $locale] = $locale;
                    $locale = $this->getParentLocaleOf($locale);
                }
            }
        }
        $locale = $this->configuration->getDefaultLocale();
        $localeChain[(string) $locale] = $locale;
        return $localeChain;
    }

Usage Example

 /**
  * Return the json array for a given locale, sourceCatalog, xliffPath and package.
  * The json will be cached.
  *
  * @param Locale $locale The locale
  * @return Result
  * @throws Exception
  */
 public function getCachedJson(Locale $locale)
 {
     $cacheIdentifier = md5($locale);
     if ($this->xliffToJsonTranslationsCache->has($cacheIdentifier)) {
         $json = $this->xliffToJsonTranslationsCache->get($cacheIdentifier);
     } else {
         $labels = [];
         $localeChain = $this->localizationService->getLocaleChain($locale);
         foreach ($this->packagesRegisteredForAutoInclusion as $packageKey => $sourcesToBeIncluded) {
             if (!is_array($sourcesToBeIncluded)) {
                 continue;
             }
             $translationBasePath = Files::concatenatePaths([$this->packageManager->getPackage($packageKey)->getResourcesPath(), $this->xliffBasePath]);
             // We merge labels in the chain from the worst choice to best choice
             foreach (array_reverse($localeChain) as $allowedLocale) {
                 $localeSourcePath = Files::getNormalizedPath(Files::concatenatePaths([$translationBasePath, $allowedLocale]));
                 foreach ($sourcesToBeIncluded as $sourceName) {
                     foreach (glob($localeSourcePath . $sourceName . '.xlf') as $xliffPathAndFilename) {
                         $xliffPathInfo = pathinfo($xliffPathAndFilename);
                         $sourceName = str_replace($localeSourcePath, '', $xliffPathInfo['dirname'] . '/' . $xliffPathInfo['filename']);
                         $labels = Arrays::arrayMergeRecursiveOverrule($labels, $this->parseXliffToArray($xliffPathAndFilename, $packageKey, $sourceName));
                     }
                 }
             }
         }
         $json = json_encode($labels);
         $this->xliffToJsonTranslationsCache->set($cacheIdentifier, $json);
     }
     return $json;
 }