浏览代码

[Translation] added a MessageCatalogue::hasStrict() method to check if a string has a translation (but without taking into account the fallback mechanism)

Fabien Potencier 13 年之前
父节点
当前提交
79710edb8a

+ 16 - 0
src/Symfony/Component/Translation/MessageCatalogue.php

@@ -92,6 +92,22 @@ class MessageCatalogue implements MessageCatalogueInterface
      * @api
      */
     public function has($id, $domain = 'messages')
+    {
+        if (isset($this->messages[$domain][$id])) {
+            return true;
+        }
+
+        if (null !== $this->fallbackCatalogue) {
+            return $this->fallbackCatalogue->has($id, $domain);
+        }
+
+        return false;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function hasStrict($id, $domain = 'messages')
     {
         return isset($this->messages[$domain][$id]);
     }

+ 12 - 0
src/Symfony/Component/Translation/MessageCatalogueInterface.php

@@ -76,6 +76,18 @@ interface MessageCatalogueInterface
      */
     function has($id, $domain = 'messages');
 
+    /**
+     * Checks if a message has a translation (it does not take into account the fallback mechanism).
+     *
+     * @param string $id     The message id
+     * @param string $domain The domain name
+     *
+     * @return Boolean true if the message has a translation, false otherwise
+     *
+     * @api
+     */
+    function hasStrict($id, $domain = 'messages');
+
     /**
      * Gets a message translation.
      *

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

@@ -142,7 +142,7 @@ class Translator implements TranslatorInterface
             $this->loadCatalogue($locale);
         }
 
-        if (!$this->catalogues[$locale]->has((string) $id, $domain)) {
+        if (!$this->catalogues[$locale]->hasStrict((string) $id, $domain)) {
             // we will use the fallback
             $locale = $this->computeFallbackLocale($locale);
         }