我有一个使用 gettext 将英语翻译成日语的项目,但是。这是我的代码.py文件导入获取文本
t=gettext.translation('noname','advbus/locale', languages=['ja'])
_=t.gettext
print _("Hello")
并在文件 .po 中
msgid "Hello"
msgstr "今日は"
我将 .po 文件编译为 .mop 文件,但它没有显示正确的今日は。(UTF8)当我运行.py文件时,它运行正常。但不对,我的意思是它不能显示 unicode (今日ã? ̄)我尝试为文件 .mo 文件设置 utf8。但是错误
File "C:envScriptspserve-script.py", line 8, in <module>
load_entry_point('pyramid==1.4a1', 'console_scripts', 'pserve')()
File "C:envlibsite-packagespyramid-1.4a1-py2.7.eggpyramidscriptspserve.py", line 47, in main
return command.run()
File "C:envlibsite-packagespyramid-1.4a1-py2.7.eggpyramidscriptspserve.py", line 290, in run
relative_to=base, global_conf=vars)
File "C:envlibsite-packagespyramid-1.4a1-py2.7.eggpyramidscriptspserve.py", line 318, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "C:envlibsite-packagespastedeployloadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "C:envlibsite-packagespastedeployloadwsgi.py", line 272, in loadobj
return context.create()
File "C:envlibsite-packagespastedeployloadwsgi.py", line 710, in create
return self.object_type.invoke(self)
File "C:envlibsite-packagespastedeployloadwsgi.py", line 146, in invoke
return fix_call(context.object, context.global_conf, **context.local_conf)
File "C:envlibsite-packagespastedeployutil.py", line 56, in fix_call
val = callable(*args, **kw)
File "C:envadvbusadvbus__init__.py", line 195, in main
config.scan()
File "C:envlibsite-packagespyramid-1.4a1-py2.7.eggpyramidconfig__init__.py", line 946, in scan
ignore=ignore)
File "C:envlibsite-packagesvenusian-1.0a7-py2.7.eggvenusian__init__.py", line 197, in scan
__import__(modname)
File "C:envadvbusadvbushello.py", line 3, in <module>
t=gettext.translation('noname','advbus/locale', languages=['ja'])
File "C:envlibgettext.py", line 478, in translation
t = _translations.setdefault(key, class_(fp))
File "C:envlibgettext.py", line 180, in __init__
self._parse(fp)
File "C:envlibgettext.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 文件
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
> 从 gettext.py
Bad magic number
的错误消息指向格式不正确的.mo
文件。阅读你自己的 Python gettext.py
副本,或者看看这个苹果托管的 gettext.py
副本。
我猜从 .po 到 .mo 的编译器无法正常工作。向我们展示您的 .po 编译过程,也许我们可以进一步提供帮助。
"Project-Id-Version: adv 0.0n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESSn"
"POT-Creation-Date: 2012-12-01 11:41+0700n"
"PO-Revision-Date: 2012-12-01 09:58+0700n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>n"
"Language-Team: ja <LL@li.org>n"
"Plural-Forms: nplurals=1; plural=0n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=utf-8n"
"Content-Transfer-Encoding: 8bitn"
"Generated-By: Babel 0.9.6n"
#: advbus/hello.py:5
msgid "Hello"
msgstr "今日は"
那是我的 .po 文件还有我的 .mo 文件今日ã ̄字符串不正确我尝试运行.py文件,它可以工作,但字符串相同 今日ã ̄所以我为 .mo 文件设置 utf-8,字符串显示在 .mo 文件上正常。但是当我运行.py文件时,上面有错误