Skip to content

Commit

Permalink
Load translations from Translator's message catalogue
Browse files Browse the repository at this point in the history
  • Loading branch information
drola committed Oct 20, 2023
1 parent fdaddb7 commit edd2c02
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 360 deletions.
13 changes: 0 additions & 13 deletions BazingaJsTranslationBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
75 changes: 11 additions & 64 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/BazingaJsTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function load(array $configs, ContainerBuilder $container)

$container
->getDefinition('bazinga.jstranslation.controller')
->replaceArgument(5, $config['locale_fallback'])
->replaceArgument(6, $config['default_domain'])
->replaceArgument(7, $config['http_cache_time']);
->replaceArgument(4, $config['locale_fallback'])
->replaceArgument(5, $config['default_domain'])
->replaceArgument(6, $config['http_cache_time']);

// Add fallback locale to active locales if missing
if ($config['active_locales'] && !in_array($config['locale_fallback'], $config['active_locales'])) {
Expand Down
44 changes: 0 additions & 44 deletions DependencyInjection/Compiler/AddLoadersPass.php

This file was deleted.

64 changes: 0 additions & 64 deletions DependencyInjection/Compiler/TranslationResourceFilesPass.php

This file was deleted.

Loading

0 comments on commit edd2c02

Please sign in to comment.