/** * Finds a localized file by working down the language-tag using the apps * associated locale and finally english as fallback * * @param string $file resource path, must contain {locale} as placeholder * @return string */ public function findLocaleFile($file) { $app = $this->service->getKernel()->getApplication(); $locale = $app->getLocalization()->getLocale(); $repo = $this->service->getResourceRepository(); $workDownLanguageTag = function ($locale, $next) use($repo, $file) { // check if the locale has more than one subtag to work down if (strpos($locale, '-') === false) { return null; } // drop the last subtag $locale = implode('-', array_slice(explode('-', $locale), 0, -1)); $filename = str_replace('{locale}', $locale, $file); if (!$repo->contains($filename)) { $filename = $next($locale, $next); } return $filename; }; $filename = $workDownLanguageTag($locale, $workDownLanguageTag); if ($filename === null) { $filename = str_replace('{locale}', 'en', $file); if (!$repo->contains($filename)) { $filename = null; } } return $filename; }
/** * {@inheritDoc} * @see \Symfony\Component\Translation\Loader\LoaderInterface::load() */ public function load($resource, $locale, $domain = 'messages') { $repo = $this->service->getResourceRepository(); if (!$repo->contains($resource)) { throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } // find file in puli repo $file = $repo->get($resource); $json = $file->getBody(); $data = Json::decode($json); $messages = []; // flatten plural strings foreach ($data as $key => $value) { if (is_array($value)) { $vals = []; foreach ($value as $k => $v) { $vals[] = sprintf('%s: %s', $k, $v); } $val = implode('|', $vals); } else { $val = $value; } $messages[$key] = str_replace(['{{', '}}'], '%', $val); } // put them into message catalog $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); return $catalogue; }
private function getFile($packageName) { $repo = $this->service->getResourceRepository(); try { return $repo->get('/packages/' . $packageName . '/composer.json'); } catch (ResourceNotFoundException $e) { throw new PackageException(sprintf('Package (%s) not found', $packageName)); } }