-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load translations from Translator's message catalogue
- Loading branch information
Showing
12 changed files
with
95 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,11 @@ | |
|
||
namespace Bazinga\Bundle\JsTranslationBundle; | ||
|
||
use Bazinga\Bundle\JsTranslationBundle\DependencyInjection\Compiler\TranslationResourceFilesPass; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Bazinga\Bundle\JsTranslationBundle\DependencyInjection\Compiler\AddLoadersPass; | ||
|
||
/** | ||
* @author William DURAND <[email protected]> | ||
*/ | ||
class BazingaJsTranslationBundle extends Bundle | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function build(ContainerBuilder $container) | ||
{ | ||
parent::build($container); | ||
|
||
$container->addCompilerPass(new AddLoadersPass()); | ||
$container->addCompilerPass(new TranslationResourceFilesPass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,44 +2,30 @@ | |
|
||
namespace Bazinga\Bundle\JsTranslationBundle\Controller; | ||
|
||
use Bazinga\Bundle\JsTranslationBundle\Finder\TranslationFinder; | ||
use Bazinga\Bundle\JsTranslationBundle\Util; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Config\ConfigCache; | ||
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\Filesystem\Exception\IOException; | ||
use Symfony\Component\Translation\TranslatorBagInterface; | ||
use Twig\Environment; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
/** | ||
* @author William DURAND <[email protected]> | ||
*/ | ||
class Controller | ||
{ | ||
/** | ||
* @var TranslatorInterface | ||
* @var TranslatorBagInterface | ||
*/ | ||
private $translator; | ||
private $translatorBag; | ||
|
||
/** | ||
* @var Environment | ||
*/ | ||
private $twig; | ||
|
||
/** | ||
* @var TranslationFinder | ||
*/ | ||
private $translationFinder; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $loaders = array(); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
|
@@ -65,53 +51,32 @@ class Controller | |
private $httpCacheTime; | ||
|
||
/** | ||
* @param TranslatorInterface $translator The translator. | ||
* @param Environment $twig The twig environment. | ||
* @param TranslationFinder $translationFinder The translation finder. | ||
* @param TranslatorBagInterface $translatorBag The translator bag. | ||
* @param Environment $twig The twig environment. | ||
* @param string $cacheDir | ||
* @param boolean $debug | ||
* @param string $localeFallback | ||
* @param string $defaultDomain | ||
* @param int $httpCacheTime | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct( | ||
$translator, | ||
TranslatorBagInterface $translatorBag, | ||
Environment $twig, | ||
TranslationFinder $translationFinder, | ||
$cacheDir, | ||
$debug = false, | ||
$localeFallback = '', | ||
$defaultDomain = '', | ||
$httpCacheTime = 86400 | ||
) { | ||
if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) { | ||
throw new \InvalidArgumentException(sprintf('Providing an instance of "%s" as translator is not supported.', get_class($translator))); | ||
} | ||
|
||
$this->translator = $translator; | ||
$this->translatorBag = $translatorBag; | ||
$this->twig = $twig; | ||
$this->translationFinder = $translationFinder; | ||
$this->cacheDir = $cacheDir; | ||
$this->debug = $debug; | ||
$this->localeFallback = $localeFallback; | ||
$this->defaultDomain = $defaultDomain; | ||
$this->httpCacheTime = $httpCacheTime; | ||
} | ||
|
||
/** | ||
* Add a translation loader if it does not exist. | ||
* | ||
* @param string $id The loader id. | ||
* @param LoaderInterface $loader A translation loader. | ||
*/ | ||
public function addLoader($id, $loader) | ||
{ | ||
if (!array_key_exists($id, $this->loaders)) { | ||
$this->loaders[$id] = $loader; | ||
} | ||
} | ||
|
||
public function getTranslationsAction(Request $request, $domain, $_format) | ||
{ | ||
$locales = $this->getLocales($request); | ||
|
@@ -134,28 +99,10 @@ public function getTranslationsAction(Request $request, $domain, $_format) | |
foreach ($locales as $locale) { | ||
$translations[$locale] = array(); | ||
|
||
$files = $this->translationFinder->get($domain, $locale); | ||
|
||
foreach ($files as $filename) { | ||
[$currentDomain] = Util::extractCatalogueInformationFromFilename($filename); | ||
|
||
if (!isset($translations[$locale][$currentDomain])) { | ||
$translations[$locale][$currentDomain] = array(); | ||
} | ||
|
||
$extension = pathinfo($filename, \PATHINFO_EXTENSION); | ||
|
||
if (isset($this->loaders[$extension])) { | ||
$resources[] = new FileResource($filename); | ||
$catalogue = $this->loaders[$extension] | ||
->load($filename, $locale, $currentDomain); | ||
|
||
$translations[$locale][$currentDomain] = array_replace_recursive( | ||
$translations[$locale][$currentDomain], | ||
$catalogue->all($currentDomain) | ||
); | ||
} | ||
} | ||
$translations[$locale] = array_merge( | ||
$translations[$locale], | ||
Util::getMessagesFromTranslatorBag($this->translatorBag, $locale, $domain) | ||
); | ||
} | ||
|
||
$content = $this->twig->render('@BazingaJsTranslation/getTranslations.' . $_format . '.twig', array( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
DependencyInjection/Compiler/TranslationResourceFilesPass.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.