public function getTranslationsAction(Request $request, $domain, $_format) { $localeCodes = $this->createLocaleCodesFromRequest($request); $cache = new ConfigCache($this->createCacheFilename($domain, $localeCodes, $_format), $this->debug); if (!$cache->isFresh()) { //Load translations: $resources = array(); $translations = array(); foreach ($localeCodes as $localeCode) { $symfonyLocaleCodeStrings = array($localeCode->getString() => $localeCode->getSymfonyString()); //If the locale code has a country code (i.e. a second part, as in "en-GB", for example) then also fetch //the 'base' translations. if ($localeCode->hasCountryCode()) { $symfonyLocaleCodeStrings[$localeCode->getLanguageCode()] = $localeCode->getLanguageCode(); } foreach ($symfonyLocaleCodeStrings as $localeCodeString => $symfonyLocaleCodeString) { $translations[$localeCodeString] = array(); $files = $this->translationFinder->get($domain, $symfonyLocaleCodeString); if (1 > count($files)) { continue; } $translations[$localeCodeString][$domain] = array(); foreach ($files as $file) { /*@var $file \Symfony\Component\Finder\SplFileInfo*/ $extension = $file->getExtension(); if (!isset($this->loaders[$extension])) { continue; } $resources[] = new FileResource($file->getPath()); $catalogue = $this->loaders[$extension]->load($file, $symfonyLocaleCodeString, $domain); $translations[$localeCodeString][$domain] = array_replace_recursive($translations[$localeCodeString][$domain], $catalogue->all($domain)); } } } //Compile a final list of translations containing - apparently - only translations for the requested //locales: $requestedTranslations = array(); foreach ($localeCodes as $localeCode) { $localeCodeString = $localeCode->getString(); $baseTranslations = array(); if ($localeCode->hasCountryCode()) { $baseTranslations = $translations[$localeCode->getLanguageCode()]; } $requestedTranslations[$localeCodeString] = array_replace_recursive($baseTranslations, $translations[$localeCodeString]); } //Render, and then cache, content for the response: $content = $this->engine->render("BazingaJsTranslationBundle::getTranslations.{$_format}.twig", array('fallback' => $this->localeFallback, 'defaultDomain' => $this->defaultDomain, 'translations' => $requestedTranslations, 'include_config' => true)); try { $cache->write($content, $resources); } catch (IOException $e) { throw new NotFoundHttpException(); } } $response = $this->createResponse($request, file_get_contents((string) $cache), $_format); return $response; }
public function getTranslationsAction(Request $request, $domain, $_format) { $locales = $this->getLocales($request); if (0 === count($locales)) { throw new NotFoundHttpException(); } $cache = new ConfigCache(sprintf('%s/%s.%s.%s', $this->cacheDir, $domain, implode('-', $locales), $_format), $this->debug); if (!$cache->isFresh()) { $resources = array(); $translations = array(); foreach ($locales as $locale) { $translations[$locale] = array(); $files = $this->translationFinder->get($domain, $locale); if (1 > count($files)) { continue; } $translations[$locale][$domain] = array(); foreach ($files as $filename) { $extension = pathinfo($filename, \PATHINFO_EXTENSION); if (isset($this->loaders[$extension])) { $resources[] = new FileResource($filename); $catalogue = $this->loaders[$extension]->load($filename, $locale, $domain); $translations[$locale][$domain] = array_replace_recursive($translations[$locale][$domain], $catalogue->all($domain)); } } } $content = $this->engine->render('BazingaJsTranslationBundle::getTranslations.' . $_format . '.twig', array('fallback' => $this->localeFallback, 'defaultDomain' => $this->defaultDomain, 'translations' => $translations, 'include_config' => true)); try { $cache->write($content, $resources); } catch (IOException $e) { throw new NotFoundHttpException(); } } if (method_exists($cache, 'getPath')) { $cachePath = $cache->getPath(); } else { $cachePath = (string) $cache; } $expirationTime = new \DateTime(); $expirationTime->modify('+' . $this->httpCacheTime . ' seconds'); $response = new Response(file_get_contents($cachePath), 200, array('Content-Type' => $request->getMimeType($_format))); $response->prepare($request); $response->setPublic(); $response->setETag(md5($response->getContent())); $response->isNotModified($request); $response->setExpires($expirationTime); return $response; }
/** * @return array */ private function getTranslations() { $translations = array(); $activeLocales = $this->activeLocales; $activeDomains = $this->activeDomains; foreach ($this->finder->all() as $filename) { list($extension, $locale, $domain) = $this->getFileInfo($filename); if (count($activeLocales) > 0 && !in_array($locale, $activeLocales) || count($activeDomains) > 0 && !in_array($domain, $activeDomains)) { continue; } if (!isset($translations[$locale])) { $translations[$locale] = array(); } if (!isset($translations[$locale][$domain])) { $translations[$locale][$domain] = array(); } if (isset($this->loaders[$extension])) { $catalogue = $this->loaders[$extension]->load($filename, $locale, $domain); $translations[$locale][$domain] = array_replace_recursive($translations[$locale][$domain], $catalogue->all($domain)); } } return $translations; }