0

I am trying to add an external locale directory from the pycountry package.

Before initializing Flask Babel, I do the following:

import pycountry
app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations;' + pycountry.LOCALES_DIR

But alas, this does not seem to be enough. For example, gettext('Germany') will not find the translation.

I think the problem might be how translations are structured in pycountry.

~/.local/lib/python3.5/site-packages/pycountry/locales/pt/LC_MESSAGES$ ls
iso15924.mo   iso3166-3.mo  iso4217.mo   iso639-3.mo
iso3166-1.mo  iso3166.mo    iso639_3.mo

Do I need to specify I want, e.g., the iso3166 file? Please see the following reference.

1 Answers1

0

I also needed to load pycountry locale with flask babel.

To do that, I look into flask-babel get_translations() about how they load translations.

Anyway, I have something working putting this somewhere in your app.

def hack_country_gettext(string):
    translations = support.Translations()
    catalog = translations.load(pycountry.LOCALES_DIR, [get_locale()], 'iso3166')
    translations.merge(catalog)
    return translations.ugettext(string)

and instead of _('Germany') use the hack function hack_country_gettext('Germany')

Sovanna
  • 78
  • 5