I'm working on Google App Engine Python3.7 I wrote this working code based on what I learned here:
http://babel.pocoo.org/en/latest/api/messages/mofile.html https://docs.python.org/3/library/gettext.html#gettext.NullTranslations.install
import babel
from babel.messages import Catalog
from babel.messages.mofile import write_mo
import gettext
_ = gettext.gettext
from gettext import GNUTranslations
from babel._compat import BytesIO
logging.info(_('gg'))
catalog = Catalog(locale='en_US', domain='myapp')
catalog.add('foo', 'Voh')
catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz'))
catalog.add('fuz', 'Futz', flags=['fuzzy'])
catalog.add('Fizz', '')
catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
logging.debug(catalog)
logging.debug(catalog['foo'].string)
buf = BytesIO()
write_mo(buf, catalog)
buf.seek(0)
translations = GNUTranslations(fp=buf)
logging.debug(translations.gettext('foo'))
logging.debug(translations.ngettext('bar', 'baz', 1))
logging.debug(translations.ngettext('bar', 'baz', 2))
logging.debug(translations.gettext('fuz'))
logging.debug(translations.gettext('Fizz'))
logging.debug(translations.gettext('Fuzz'))
logging.debug(translations.gettext('Fuzzes'))
logging.debug(_('foo'))#this returns 'foo'
Then i tried to add:
lang1 = gettext.translation('myapp', languages=['en'])
lang1.install()
logging.debug(_('foo'))
but it doesn't work. It raises the Error:
FileNotFoundError: [Errno 2] No translation file found for domain: 'myapp'