0

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'
Machavity
  • 30,841
  • 27
  • 92
  • 100
Paolo177
  • 366
  • 6
  • 18
  • Can you share the directory structure for your app? Do you have a `myapp.mo`/`myapp.po` file for your domain? – Dustin Ingram Jan 03 '19 at 21:35
  • @DustinIngram I don't have any of them as I'm trying to contruct them within my app itself. But maybe it's impossible. For what I understand the .mo file created by the write_mo() function is just cached but not saved anywere that's why I think the added code doesn't work but I don't know how to save it. – Paolo177 Jan 03 '19 at 21:44

0 Answers0