Search code examples
pythonflaskinternationalizationpython-babelflask-babel

Flask Babel Translations path


I have a webapp that uses Flask Babel to translate the templates. This webapp can use multiple databases by adding the database name to its url, like:

myapp.com/<dbname>

The problem is that translations path is hardcoded in babel:

    def list_translations(self):
        """Returns a list of all the locales translations exist for.  The
        list returned will be filled with actual locale objects and not just
        strings.

        .. versionadded:: 0.6
        """
        dirname = os.path.join(self.app.root_path, 'translations')
        if not os.path.isdir(dirname):
            return []
        result = []
        for folder in os.listdir(dirname):
            locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
            if not os.path.isdir(locale_dir):
                continue
            if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
                result.append(Locale.parse(folder))
        if not result:
            result.append(Locale.parse(self._default_locale))
        return result

And babel is forcing me to the directory named "translations" and to the language file named "messages.mo"

I tried all over the internet, but still no clear solution for this problem.

One idea I had in mind, is it possible to change babel with babelex and then I can override translations path?


Solution

  • The solution was to install Flask Babelex instead of Babel.