Browse Source

Fixed loading from plugin translations

sh4nks 10 years ago
parent
commit
caf1f8ac66

+ 2 - 3
flaskbb/plugins/portal/translations/en/LC_MESSAGES/messages.po

@@ -3,12 +3,11 @@
 # This file is distributed under the same license as the PROJECT project.
 # FIRST AUTHOR <EMAIL@ADDRESS>, 2015.
 #
-#, fuzzy
 msgid ""
 msgstr ""
 "Project-Id-Version: PROJECT VERSION\n"
 "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
-"POT-Creation-Date: 2015-01-05 21:38+0100\n"
+"POT-Creation-Date: 2015-02-13 14:26+0100\n"
 "PO-Revision-Date: 2015-01-05 21:38+0100\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: en <LL@li.org>\n"
@@ -22,5 +21,5 @@ msgstr ""
 msgid ""
 "Please install the plugin first to configure the forums which should be "
 "displayed"
-msgstr ""
+msgstr "Please install the plugin first in order to configure which forums are displayed."
 

+ 6 - 6
flaskbb/utils/translations.py

@@ -1,7 +1,6 @@
 import os
 
 import babel
-from flask import _request_ctx_stack
 
 from flask_babelex import Domain, get_locale
 from flask_plugins import get_plugins_list
@@ -20,6 +19,7 @@ class FlaskBBDomain(Domain):
         self.flaskbb_translations = os.path.join(
             self.app.root_path, "translations"
         )
+
         # Plugin translations
         with self.app.app_context():
             self.plugin_translations = [
@@ -33,12 +33,7 @@ class FlaskBBDomain(Domain):
         object if used outside of the request or if a translation cannot be
         found.
         """
-        ctx = _request_ctx_stack.top
-        if ctx is None:
-            return babel.support.NullTranslations()
-
         locale = get_locale()
-
         cache = self.get_translations_cache()
 
         translations = cache.get(str(locale))
@@ -50,6 +45,11 @@ class FlaskBBDomain(Domain):
                 domain="messages"
             )
 
+            # If no compiled translations are found, return the
+            # NullTranslations object.
+            if not isinstance(translations, babel.support.Translations):
+                return translations
+
             # now load and add the plugin translations
             for plugin in self.plugin_translations:
                 plugin_translation = babel.support.Translations.load(

+ 25 - 5
tests/unit/utils/test_translations.py

@@ -1,13 +1,33 @@
+import subprocess
+import os
 from flask import current_app
+from babel.support import Translations, NullTranslations
 from flaskbb.utils.translations import FlaskBBDomain
+from flaskbb.extensions import plugin_manager
 
 
-def test_flaskbbdomain_translations():
+def _compile_translations():
+    PLUGINS_FOLDER = os.path.join(current_app.root_path, "plugins")
+    translations_folder = os.path.join(current_app.root_path, "translations")
+    subprocess.call(["pybabel", "compile", "-d", translations_folder])
+
+    for plugin in plugin_manager.all_plugins:
+        plugin_folder = os.path.join(PLUGINS_FOLDER, plugin)
+        translations_folder = os.path.join(plugin_folder, "translations")
+        subprocess.call(["pybabel", "compile", "-d", translations_folder])
+
+
+def test_flaskbbdomain_translations(default_settings):
     domain = FlaskBBDomain(current_app)
 
-    assert domain.get_translations_cache() == {}
+    with current_app.test_request_context():
+        assert domain.get_translations_cache() == {}
+
+        # no compiled translations are available
+        assert isinstance(domain.get_translations(), NullTranslations)
 
-    # returns an translation object
-    assert domain.get_translations() is not None
+        # lets compile them and test again
+        _compile_translations()
 
-    assert len(domain.get_translations_cache()) > 0
+        # now there should be translations :)
+        assert isinstance(domain.get_translations(), Translations)