代码之家  ›  专栏  ›  技术社区  ›  Paolo177

谷歌应用引擎-Python-Babel-write\u mo()

  •  0
  • Paolo177  · 技术社区  · 7 年前

    我在谷歌应用引擎Python3.7上工作

    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'
    

    然后我试图补充:

    lang1 = gettext.translation('myapp', languages=['en'])
    lang1.install()
    logging.debug(_('foo'))
    

    但它不起作用。它引发了一个错误:

    FileNotFoundError: [Errno 2] No translation file found for domain: 'myapp'
    
    0 回复  |  直到 5 年前