Python和gettext [英] Python and gettext

查看:50
本文介绍了Python和gettext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个利用一堆翻译后的字符串的Python应用程序.包含上述字符串的目录结构如下:

 /语言环境default.pot#参考英文字符串在这里/es_ES/LC_MESSAGESdefault.po#西班牙字符串/de_DE/LC_MESSAGESdefault.po#德语字符串 

这些 default.po 文件是由PHP应用程序生成的,但据我所知,它们符合使用 gettext 实现所需的通用标准./p>

当我尝试使用gettext在Python中使用这些字符串时,以下内容将消失(此示例在 locales 目录中运行:

 >>>进口操作系统;os.listdir('.')['.svn','de_DE','default.pot','eng','es_ES','fr_FR','ja_JP','ko_KR','pl_PL','pt_BR','ru_RU']>>>导入os.path>>>os.path.exists('./es_ES/LC_MESSAGES/default.po')真的>>>导入gettext>>>ldir ='es_ES/LC_MESSAGES/'>>>t = gettext.translation('default',ldir)追溯(最近一次通话):在< module>中的文件< stdin>",第1行,翻译文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gettext.py",第469行IOError:[Errno 2]找不到域的翻译文件:默认">>> 

我不确定自己在做什么错(除了对该库缺乏经验以及上下文中的域"的概念之外.)

我犯了一个简单的错误吗?还是我对这种垃圾的工作方式有根本的缺陷?

谢谢!

解决方案

对此我感到非常生疏,但基于过去的经验和 http://docs.python.org/library/gettext ,在这里我可以看到缺少的两个主要内容:

  1. ldir应该设置为包含语言环境数据的基本目录(/usr/share/locale是经典的系统位置).gettext将搜索ldir/lang_COUNTRY/catalog.mo
  2. 无法单独读取.po文件,您需要将其转换为.mo二进制文件.通常,您会使用msgfmt来执行此操作(在Mac上,"brew install gettext"是最简单的方法).

一个简单的例子:

  $ find/tmp/locales -type f/tmp/locales/de_DE/LC_MESSAGES/default.mo/tmp/locales/de_DE/LC_MESSAGES/default.po/tmp/locales/default.pot/tmp/locales/en_IE/LC_MESSAGES/default.mo/tmp/locales/en_IE/LC_MESSAGES/default.po$〜/Library/homebrew/Cellar/gettext/0.18.1.1/bin/msgfmt \-o语言环境/en_IE/LC_MESSAGES/default.mo \语言环境/en_IE/LC_MESSAGES/default.po$ cat/tmp/app.py导入gettextt = gettext.translation('default',"/tmp/locales")_ = t.ugettext打印_("Hello World")$地区LANG ="en_IE.UTF-8"LC_COLLATE ="en_IE.UTF-8"LC_CTYPE ="en_IE.UTF-8"LC_MESSAGES ="en_IE.UTF-8"LC_MONETARY ="en_IE.UTF-8"LC_NUMERIC ="en_IE.UTF-8"LC_TIME ="en_IE.UTF-8"LC_ALL =$ python app.py感觉如何?$ LC_MESSAGES = de_DE python app.py古登标签 

I'm building a Python application that utilizes a bunch of translated strings. The directory structure housing said strings looks like this:

/locales
    default.pot # reference English strings live here
    /es_ES
        /LC_MESSAGES
            default.po #Spanish strings
    /de_DE
        /LC_MESSAGES
            default.po #German strings

These default.po files were generated by a PHP application, but as far as I know, they conform to the general standard needed to work with gettext implementations.

When I attempt to utilize these strings in Python using gettext, the following goes down (this example was run from within the locales directory:

>>> import os; os.listdir('.')
['.svn', 'de_DE', 'default.pot', 'eng', 'es_ES', 'fr_FR', 'ja_JP', 'ko_KR', 'pl_PL', 'pt_BR', 'ru_RU']
>>> import os.path
>>> os.path.exists('./es_ES/LC_MESSAGES/default.po')
True
>>> import gettext
>>> ldir = 'es_ES/LC_MESSAGES/'
>>> t = gettext.translation('default',ldir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gettext.py", line 469, in translation
IOError: [Errno 2] No translation file found for domain: 'default'
>>>

I'm not sure what I'm doing wrong here (beyond inexperience with this library and the notion of 'domain' in its context).

Am I making a simple mistake? Or do I have a fundamental flaw in my understanding of how this crap works?

Thanks!

解决方案

I'm very rusty on this, but based on past experience and http://docs.python.org/library/gettext, I can see two main things missing here:

  1. ldir should be set to the base directory containing locale data (/usr/share/locale is the classic system location). gettext will search for ldir/lang_COUNTRY/catalog.mo
  2. The .po files can't be read on their own, you need to convert them to .mo binary files. Typically you'd use msgfmt to do this ("brew install gettext" on the mac is the easiest way to get this).

A quick example:

$ find /tmp/locales -type f
/tmp/locales/de_DE/LC_MESSAGES/default.mo
/tmp/locales/de_DE/LC_MESSAGES/default.po
/tmp/locales/default.pot
/tmp/locales/en_IE/LC_MESSAGES/default.mo
/tmp/locales/en_IE/LC_MESSAGES/default.po

$ ~/Library/homebrew/Cellar/gettext/0.18.1.1/bin/msgfmt \
-o locales/en_IE/LC_MESSAGES/default.mo \
locales/en_IE/LC_MESSAGES/default.po

$ cat /tmp/app.py 
import gettext
t = gettext.translation('default', "/tmp/locales")
_ = t.ugettext

print _("Hello World")

$ locale
LANG="en_IE.UTF-8"
LC_COLLATE="en_IE.UTF-8"
LC_CTYPE="en_IE.UTF-8"
LC_MESSAGES="en_IE.UTF-8"
LC_MONETARY="en_IE.UTF-8"
LC_NUMERIC="en_IE.UTF-8"
LC_TIME="en_IE.UTF-8"
LC_ALL=

$ python app.py 
How's the craic?

$ LC_MESSAGES=de_DE python app.py
Guten Tag

这篇关于Python和gettext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆