Neos\Neos\Service\XliffService::getCachedJson PHP Method

getCachedJson() public method

The json will be cached.
public getCachedJson ( Locale $locale ) : Neos\Error\Messages\Result
$locale Neos\Flow\I18n\Locale The locale
return Neos\Error\Messages\Result
    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;
    }

Usage Example

 /**
  * Returns the cached json array with the xliff labels
  *
  * @param string $locale
  * @return string
  */
 public function xliffAsJsonAction($locale)
 {
     $this->response->setHeader('Content-Type', 'application/json');
     return $this->xliffService->getCachedJson(new Locale($locale));
 }