Ver código fonte

made locale determination for translation lazy-loaded

This allows to have a stateless-website (without any cookie)
for instance if the locale is part of the domain name
and not attached to the user.
Fabien Potencier 14 anos atrás
pai
commit
dbde494424

+ 14 - 4
src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

@@ -26,6 +26,7 @@ class Translator extends BaseTranslator
 {
     protected $container;
     protected $options;
+    protected $session;
 
     /**
      * Constructor.
@@ -42,10 +43,7 @@ class Translator extends BaseTranslator
      */
     public function __construct(ContainerInterface $container, MessageSelector $selector, array $options = array(), Session $session = null)
     {
-        if (null !== $session) {
-            parent::__construct($session->getLocale(), $selector);
-        }
-
+        $this->session = $session;
         $this->container = $container;
 
         $this->options = array(
@@ -61,6 +59,18 @@ class Translator extends BaseTranslator
         $this->options = array_merge($this->options, $options);
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getLocale()
+    {
+        if (null === $this->locale && null !== $this->session) {
+            $this->locale = $session->getLocale();
+        }
+
+        return $this->locale;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 2 - 2
src/Symfony/Component/Translation/Translator.php

@@ -99,7 +99,7 @@ class Translator implements TranslatorInterface
     public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
     {
         if (!isset($locale)) {
-            $locale = $this->locale;
+            $locale = $this->getLocale();
         }
 
         if (!isset($this->catalogues[$locale])) {
@@ -115,7 +115,7 @@ class Translator implements TranslatorInterface
     public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
     {
         if (!isset($locale)) {
-            $locale = $this->locale;
+            $locale = $this->getLocale();
         }
 
         if (!isset($this->catalogues[$locale])) {