python 生成本地化文件的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/739314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to generate localization files
提问by landyman
I'm currently writing an app in Python and need to provide localization for it.
我目前正在用 Python 编写一个应用程序,需要为其提供本地化。
I can use gettext and the utilities that come with it to generate .po and .mo files. But editing the .po files for each language, one-by-one, seems a bit tedious. Then, creating directories for each language and generating the .mo files, one-by-one, seems like overkill. The end result being something like:
我可以使用 gettext 和它附带的实用程序来生成 .po 和 .mo 文件。但是逐一编辑每种语言的 .po 文件似乎有点乏味。然后,为每种语言创建目录并逐个生成 .mo 文件似乎有点过头了。最终结果类似于:
/en_US/LC_MESSAGES/en_US.mo
/en_CA/LC_MESSAGES/en_CA.mo
etc.
I could be wrong, but it seems like there's got to be a better way to do this. Does anyone have any tools, tricks or general knowledge that I haven't found yet?
我可能是错的,但似乎必须有更好的方法来做到这一点。有没有人有任何我还没有找到的工具、技巧或常识?
Thanks in advance!
提前致谢!
EDIT: To be a little more clear, I'm looking for something that speeds up the process. since it's already pretty easy. For example, in .NET, I can generate all of the strings that need to be translated into an excel file. Then, translators can fill out the excel file and add columns for each language. Then, I can use xls2resx to generate the language resource files. Is there anything like that for gettext? I realize I could write a script to create and read from a csv and generate the files -- I was just hoping there is something already made.
编辑:更清楚一点,我正在寻找可以加快进程的东西。因为这已经很容易了。例如,在 .NET 中,我可以生成所有需要转换为 excel 文件的字符串。然后,翻译人员可以填写 excel 文件并为每种语言添加列。然后,我可以使用 xls2resx 生成语言资源文件。gettext 有没有类似的东西?我意识到我可以编写一个脚本来创建和读取 csv 并生成文件——我只是希望已经有了一些东西。
回答by warp
I've just done this for a project I'm working on, for us the process is like this:
我刚刚为我正在做的一个项目完成了这项工作,对我们来说,过程是这样的:
First, I have a POTFILES.in file which contains a list of source files needing translation. We actually have two files (e.g. admin.in and user.in), as the admin interface doesn't always need translating. So we can send translators only the file containing strings the users see.
首先,我有一个 POTFILES.in 文件,其中包含需要翻译的源文件列表。我们实际上有两个文件(例如 admin.in 和 user.in),因为管理界面并不总是需要翻译。因此,我们可以仅将包含用户看到的字符串的文件发送给翻译人员。
Running the following command will create a .pot template from the POTFILES.in:
运行以下命令将从 POTFILES.in 创建一个 .pot 模板:
xgettext --files-from=POTFILES.in --directory=.. --output=messages.pot
Run the above command from the po/ directory, which should be a subdirectory of your project and contain the POTFILES.in file. The .pot file has the exact same format as a .po file, but it is a template containing no translations. You create a new template whenever new translatable strings have been added to your source files.
从 po/ 目录运行上述命令,该目录应该是您项目的子目录并包含 POTFILES.in 文件。.pot 文件的格式与 .po 文件完全相同,但它是一个不包含翻译的模板。每当将新的可翻译字符串添加到源文件时,您就创建一个新模板。
To update the already translated .po files and add new strings to them, run:
要更新已翻译的 .po 文件并向其中添加新字符串,请运行:
msgmerge --update --no-fuzzy-matching --backup=off da.po messages.pot
In the above example I disable fuzzy matching, as our strings are a mess and it did more harm than good. I also disabled backup files, as everything is in subversion here. Run the above command for each language, in this case I updated the danish translation.
在上面的例子中,我禁用了模糊匹配,因为我们的字符串一团糟,弊大于利。我还禁用了备份文件,因为这里的所有内容都处于颠覆状态。为每种语言运行上述命令,在这种情况下,我更新了丹麦语翻译。
Finally run msgfmt to create the .mo files from the .po files:
最后运行 msgfmt 从 .po 文件创建 .mo 文件:
msgfmt nl.po --output-file nl.mo
Ofcourse, you wouldn't want to run this manually everytime, so create some scripts (or preferably one or more Makefiles) to do this for you.
当然,您不想每次都手动运行它,因此创建一些脚本(或者最好是一个或多个 Makefile)来为您执行此操作。
Note that your directory layout is slightly different from my situation, so you will have to adjust output filenames/paths a bit, but the steps should be the same.
请注意,您的目录布局与我的情况略有不同,因此您必须稍微调整输出文件名/路径,但步骤应该相同。
回答by glezos
You can use Transifex, used by quite a few Python projects (Django, Mercurial). It has a web-based editor and command-line client for automation and also supports .NET, if that can be of help.
您可以使用Transifex,它被很多 Python 项目(Django、Mercurial)使用。它具有基于 Web 的编辑器和用于自动化的命令行客户端,并且还支持 .NET(如果有帮助的话)。
Disclaimer: I'm the Transifex project lead.
免责声明:我是 Transifex 项目的负责人。
回答by lothar
It eludes me how you want to achieve a real translation without editing the .po files for each language? Magic?
我不知道您想如何在不编辑每种语言的 .po 文件的情况下实现真正的翻译?魔法?
Edit (after comment) - how to automate the generation of the .pofiles:
编辑(评论后) - 如何自动生成.po文件:
I don't use Gettext, but from the wiki pageit looks like you only have to call
我不使用 Gettext,但从维基页面看起来你只需要调用
msginit --locale=xx --input=name.pot
msginit --locale=xx --input=name.pot
for every language you want to support. I would generate a script that generates all the language files, give them to the translators, and when you get them back run msgfmton them.
对于您想要支持的每种语言。我会生成一个脚本来生成所有语言文件,将它们提供给翻译人员,当你把它们取回时,对它们运行msgfmt。
回答by Sergey Stolyarov
Use scripts. Assume your source code is in the directory $SRC, so create directory $SRC/po for i18n files (pot, po, mo etc). Then create file $SRC/po/POTFILES.in where specify path to source code files with gettext function calls. It may look like:
使用脚本。假设您的源代码在 $SRC 目录中,因此为 i18n 文件(pot、po、mo 等)创建目录 $SRC/po。然后创建文件 $SRC/po/POTFILES.in,其中使用 gettext 函数调用指定源代码文件的路径。它可能看起来像:
[encoding: UTF-8]
./main.cpp
./plugins/list.cpp
Now you can use the following commands (inside directory $SRC):
现在您可以使用以下命令(在 $SRC 目录中):
For updating master-file messages.pot:
更新主文件messages.pot:
intltool-update --pot --gettext-package=messages --verbose
For merging changes from messages.pot to fr.po file, for example:
用于合并从 messages.pot 到 fr.po 文件的更改,例如:
msgmerge -U fr.po messages.pot
To generate mo file use command:
要生成 mo 文件使用命令:
msgmerge -o - fr.po messages.pot | msgfmt -c -o fr/LC_MESSAGES/programname.mo -
Of course directory fr/LC_MESSAGES/ must be created before that call, you can also delete existing fr/LC_MESSAGES/programname.mo before execution
当然目录fr/LC_MESSAGES/必须在调用之前创建,你也可以在执行前删除现有的fr/LC_MESSAGES/programname.mo
In order to speed up process place all such commands into the corresponding .sh-files for example.
例如,为了加快进程,将所有此类命令放入相应的 .sh 文件中。
回答by maciejka
You can try Translate Toolkitfor various po file conversions(po2csv for example). Child projects Pootleallows to manage translations over the web.
您可以尝试使用Translate Toolkit进行各种 po 文件转换(例如 po2csv)。子项目Pootle允许通过网络管理翻译。
回答by Van Gale
My suggestion is django-rosetta, which gives you a web based interface to editing your translations. Django doesn't need to be involved at all with your app, you're just using it for an online .PO edit facility.
我的建议是django-rosetta,它为您提供了一个基于 Web 的界面来编辑您的翻译。Django 根本不需要参与您的应用程序,您只需将其用于在线 .PO 编辑工具。
Another interesting new translation site, which I believe was built using Django/Pinax is Transifex, although I'm not sure if it allows direct editing of the .PO files.
另一个有趣的新翻译站点,我相信它是使用 Django/Pinax 构建的,是Transifex,尽管我不确定它是否允许直接编辑 .PO 文件。