Exemple #1
0
    def test_get_locale(self):
        assert babel_ext.get_locale() is None

        app = flask.Flask(__name__)
        babel_ext.Babel(app)
        with app.app_context():
            assert babel_ext.get_locale() == Locale.parse("en")
Exemple #2
0
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        locale = get_locale()
        cache = self.get_translations_cache()

        translations = cache.get(str(locale))
        if translations is None:
            # load Spotipo translations
            translations = babel.support.Translations.load(
                dirname=self.spotipo_translations,
                locales=locale,
                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(
                    dirname=plugin, locales=locale, domain="messages")
                if isinstance(plugin_translation, babel.support.Translations):
                    translations.add(plugin_translation)

            cache[str(locale)] = translations

        return translations
Exemple #3
0
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        state = get_state(silent=True)

        if state is None:
            return babel.support.NullTranslations()

        locale = get_locale()
        cache = self.get_translations_cache()
        translations = cache.get(str(locale))

        # load them into the cache
        if translations is None:
            dirname = self.get_translations_path(state.app)
            translations = babel.support.Translations.load(dirname,
                                                           locale,
                                                           domain=self.domain)
            # now load and add the plugin translations
            for plugin in self.plugin_translations:
                logger.debug("Loading plugin translation from: "
                             "{}".format(plugin))
                plugin_translation = babel.support.Translations.load(
                    dirname=plugin, locales=locale, domain="messages")

                if not isinstance(plugin_translation,
                                  babel.support.NullTranslations):
                    translations.add(plugin_translation)

            self.cache[str(locale)] = translations

        return translations
Exemple #4
0
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        locale = get_locale()
        cache = self.get_translations_cache()

        translations = cache.get(str(locale))
        if translations is None:
            # load flaskbb translations
            translations = babel.support.Translations.load(
                dirname=self.flaskbb_translations,
                locales=locale,
                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(
                    dirname=plugin,
                    locales=locale,
                    domain="messages"
                )
                translations.add(plugin_translation)

            cache[str(locale)] = translations

        return translations
Exemple #5
0
    def test_force_locale(self):
        app = flask.Flask(__name__)
        b = babel_ext.Babel(app)

        @b.localeselector
        def select_locale():
            return 'de_DE'

        with babel_ext.force_locale('en_US'):
            assert babel_ext.get_locale() is None

        with app.test_request_context():
            assert str(babel_ext.get_locale()) == 'de_DE'
            with babel_ext.force_locale('en_US'):
                assert str(babel_ext.get_locale()) == 'en_US'
            assert str(babel_ext.get_locale()) == 'de_DE'
Exemple #6
0
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        translations = super(FlaskBBDomain, self).get_translations()
        locale = get_locale()
        # now load and add the plugin translations
        for plugin in self.plugin_translations:
            plugin_translation = babel.support.Translations.load(
                dirname=plugin, locales=locale, domain="messages")
            translations.add(plugin_translation)

        self.cache[str(locale)] = translations
        return translations
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        locale = get_locale()
        cache = self.get_translations_cache()

        translations = cache.get(str(locale))
        if translations is None:
            # load janitoo_manager translations
            translations = babel.support.Translations.load(
                dirname=self.janitoo_manager_translations, locales=locale, domain="messages"
            )

            # If no compiled translations are found, return the
            # NullTranslations object.
            if not isinstance(translations, babel.support.Translations):
                return translations

            # Plugin translations are at the moment not supported under
            # Python 3. There is currently a bug in Babel where it is
            # not possible to merge two message catalogs.
            # https://github.com/mitsuhiko/babel/pull/92
            # So instead of adding/merging them, we are just skipping them
            # Better then no python3 support though..
            if not PY2:
                return translations

            # now load and add the plugin translations
            # ~ for plugin in self.plugin_translations:
            # ~ plugin_translation = babel.support.Translations.load(
            # ~ dirname=plugin,
            # ~ locales=locale,
            # ~ domain="messages"
            # ~ )
            # ~ translations.add(plugin_translation)

            cache[str(locale)] = translations

        return translations
Exemple #8
0
    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """
        state = get_state(silent=True)

        if state is None:
            return babel.support.NullTranslations()

        locale = get_locale()
        cache = self.get_translations_cache()
        translations = cache.get(str(locale))

        # load them into the cache
        if translations is None:
            dirname = self.get_translations_path(state.app)
            translations = babel.support.Translations.load(
                dirname,
                locale,
                domain=self.domain
            )
            # now load and add the plugin translations
            for plugin in self.plugin_translations:
                logger.debug("Loading plugin translation from: "
                             "{}".format(plugin))
                plugin_translation = babel.support.Translations.load(
                    dirname=plugin,
                    locales=locale,
                    domain="messages"
                )

                if type(plugin_translation) is not babel.support.NullTranslations:
                    translations.add(plugin_translation)

            self.cache[str(locale)] = translations

        return translations