1

there is small problem with Flask web application. Right now I'm creating backend for one website and one of the task is to create translation panel in Flask-Admin. Website are using Flask-Babel for multi language support.

Long story short, I made BaseView in Admin panel that show all translations and give ability to edit them. But there is one problem Babel reading .mo files when server start, and when my View saving translations via parsing .po files and compile them to .mo files website don't show any update until I reload it.

Is there any solution how to handle that. Maybe other module than Babel?

P.S.: I thought about (and tried to do) reloading website when admin clicking save changes in view, but it is look like stupid idea, as people on the website could be doing something and website reload will delete their data :(

sh_mykola
  • 51
  • 1
  • 4

1 Answers1

1

With Flask-Babelex (0.9.4) the catalog is loaded into the cache during the first translation in the Domain class with the get_translations function.

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.
    """
    ctx = _request_ctx_stack.top
    if ctx is None:
        return NullTranslations()

    locale = get_locale()

    cache = self.get_translations_cache(ctx)

    translations = cache.get(str(locale))

    if translations is None:
        dirname = self.get_translations_path(ctx)
        translations = support.Translations.load(dirname,
                                                 locale,
                                                 domain=self.domain)
        cache[str(locale)] = translations

    return translations

The cache can be set manually, even if it is actually a protected member. Suppose you have initiated Flask-babelex in your app as babel. Then you can do the following

from app import babel
babel._default_domain.cache = dict()

After that the cache is empty and will be saved again at the next translation.

Tested with Flask 1.1.4 and Flask-babelex 0.9.4

Tenobaal
  • 11
  • 1