首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Gettext python金字塔utf8

Gettext python金字塔utf8
EN

Stack Overflow用户
提问于 2012-12-01 12:50:40
回答 2查看 939关注 0票数 1

我有一个项目使用gettext来翻译英语到日语,但是。这是我的代码.py文件导入gettext

代码语言:javascript
运行
复制
t=gettext.translation('noname','advbus/locale', languages=['ja'])
_=t.gettext
print _("Hello")

在文件.po中

代码语言:javascript
运行
复制
msgid "Hello"
msgstr "今日は"

我将.po文件编译成.mop文件,但它没有显示正确的今日は。(Utf8)当我运行.py文件时,它运行正常。但是不正确,我的意思是它不能显示unicode (ä».moé-Šã?)我尝试为文件设置utf8文件。但是错误

代码语言:javascript
运行
复制
  File "C:\env\Scripts\pserve-script.py", line 8, in <module>
    load_entry_point('pyramid==1.4a1', 'console_scripts', 'pserve')()
  File "C:\env\lib\site-packages\pyramid-1.4a1-py2.7.egg\pyramid\scripts\pserve.py", line 47, in main
    return command.run()
  File "C:\env\lib\site-packages\pyramid-1.4a1-py2.7.egg\pyramid\scripts\pserve.py", line 290, in run
    relative_to=base, global_conf=vars)
  File "C:\env\lib\site-packages\pyramid-1.4a1-py2.7.egg\pyramid\scripts\pserve.py", line 318, in loadapp
    return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
  File "C:\env\lib\site-packages\paste\deploy\loadwsgi.py", line 247, in loadapp
    return loadobj(APP, uri, name=name, **kw)
  File "C:\env\lib\site-packages\paste\deploy\loadwsgi.py", line 272, in loadobj
    return context.create()
  File "C:\env\lib\site-packages\paste\deploy\loadwsgi.py", line 710, in create
    return self.object_type.invoke(self)
  File "C:\env\lib\site-packages\paste\deploy\loadwsgi.py", line 146, in invoke
    return fix_call(context.object, context.global_conf, **context.local_conf)
  File "C:\env\lib\site-packages\paste\deploy\util.py", line 56, in fix_call
    val = callable(*args, **kw)
  File "C:\env\advbus\advbus\__init__.py", line 195, in main
    config.scan()
  File "C:\env\lib\site-packages\pyramid-1.4a1-py2.7.egg\pyramid\config\__init__.py", line 946, in scan
    ignore=ignore)
  File "C:\env\lib\site-packages\venusian-1.0a7-py2.7.egg\venusian\__init__.py", line 197, in scan
    __import__(modname)
  File "C:\env\advbus\advbus\hello.py", line 3, in <module>
    t=gettext.translation('noname','advbus/locale', languages=['ja'])
  File "C:\env\lib\gettext.py", line 478, in translation
    t = _translations.setdefault(key, class_(fp))
  File "C:\env\lib\gettext.py", line 180, in __init__
    self._parse(fp)
  File "C:\env\lib\gettext.py", line 281, in _parse
    raise IOError(0, 'Bad magic number', filename)
IOError: [Errno 0] Bad magic number: 'advbus/locale\\ja\\LC_MESSAGES\\noname.mo'

这是我在互联网上拷贝的subscribers.py文件

代码语言:javascript
运行
复制
    from pyramid.i18n import get_localizer, TranslationStringFactory

def add_renderer_globals(event):
    request = event['request']
    event['_'] = request.translate
    event['localizer'] = request.localizer

tsf = TranslationStringFactory('YOUR_GETTEXT_DOMAIN')

def add_localizer(event):
    request = event.request
    localizer = get_localizer(request)
    def auto_translate(*args, **kwargs):
        return localizer.translate(tsf(*args, **kwargs))
    request.localizer = localizer
    request.translate = auto_translate
EN

回答 2

Stack Overflow用户

发布于 2012-12-01 13:05:59

来自gettext.py的错误消息Bad magic number指向格式不正确的.mo文件。阅读您自己的Python的gettext.py副本,或者查看this Apple-hosted copy of gettext.py

我猜从.po到.mo的编译器不能正常工作。向我们展示您的.po编译过程,也许我们可以提供进一步的帮助。

票数 1
EN

Stack Overflow用户

发布于 2012-12-03 09:27:26

代码语言:javascript
运行
复制
"Project-Id-Version: adv 0.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2012-12-01 11:41+0700\n"
"PO-Revision-Date: 2012-12-01 09:58+0700\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: ja <LL@li.org>\n"
"Plural-Forms: nplurals=1; plural=0\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"

#: advbus/hello.py:5
msgid "Hello"
msgstr "今日は"

这就是我的.po文件和我的.mo文件,字符串不正确,我试着运行.py文件,它可以工作,但是字符串相同,所以我为.mo文件设置了.mo -8,该字符串显示在Š文件上ok。但当我运行.py文件时,出现上述错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13656475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档