我是使用gettext库翻译文本的新手。我想翻译下一个文件中的文本:
多布尔斯_消息.py
def set_message(self, language):
t = gettext.translation("basketmetrics_i18n", "../i18n", fallback=True, languages=["'" + language + "'"])
t.install()
_ = t.gettext
self.message = _("mensaje_dd_1")
要做到这一点,首先我要创建多臂犬_消息.po有了这个说明:
^{pr2}$然后,我创建了这些目录结构i18n/es/LC峎MESSAGES和i18n/en/LC_消息,并为每种语言创建了.po版本,其中包含以下说明:
msginit -i dobles_message.pot -o ../i18n/es/LC_MESSAGES/basketmetrics_i18n.po -l es
msginit -i dobles_message.pot -o ../i18n/en/LC_MESSAGES/basketmetrics_i18n.po -l en
第三,我更改了字符集并翻译了每个文件的文本。 第四,我用这个指令在i18n/es/LC峎MESSAGES目录和i18n/en/LC_MESSAGES目录中从每个.po文件创建了.mo文件
msgfmt basketmetrics_i18n.po -o basketmetrics_i18n.mo
但是,当我运行应用程序而不是翻译文本时,muy应用程序会显示msgid。在
以下是我的文件和目录结构:
接下来是我的档案内容。。。在
多布尔斯_消息.pot
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr ""
basketmetrics_i18n.po(英文版)
# English translations for PACKAGE package.
# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# José Carlos <josecarlos@asterix>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: 2018-10-10 13:31+0200\n"
"Last-Translator: Jos Carlos <josecarlos@asterix>\n"
"Language-Team: English\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr "¡¡¡Hi ###twitter###!!! From www.basketmetrics.com we want to congrats to get your double double nº ###numero### this season. ¡¡¡Congratulations!!!"
basketmetrics_i18n.po(西班牙语版)
# Spanish translations for PACKAGE package.
# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# José Carlos <josecarlos@asterix>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: 2018-10-10 13:28+0200\n"
"Last-Translator: Jos Carlos <josecarlos@asterix>\n"
"Language-Team: Spanish\n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=latin-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr "¡¡¡Hola ###twitter###!!! Desde www.basketmetrics.com queremos felicitarte por haber conseguido tu doble doble nº ###numero### esta temporada. ¡¡¡Felicidades!!!"
我做错什么了?我用的是Python3
编辑I:
在我的代码中,我删除了“fallback=True”选项,得到以下错误:
在我的第一步中,我定义了域“basmetrics_i18n”,并使用该名称创建了.po文件。发生什么事了?在
编辑二:
根据@stovfl的明智建议,我更改了set_message函数:
def set_message(self, language):
print("language: " + language)
gettext.bindtextdomain('basketmetrics_i18n', '../i18n')
gettext.textdomain('basketmetrics_i18n')
_ = gettext.gettext
self.message = _("mensaje_dd_1")
print("Mensaje traducido: " + self.message)
现在,我的应用程序没有任何错误,但我仍然得到的是msgid而不是msgstr。我什么都不懂:(
根据文件:
如果您更改以下
languages=[language]
,您的代码将对我有效:最后,我找到了错误。由于语言目录所在目录的路径而导致的错误。必须从项目的根目录而不是从要翻译文本的文件指定此路由。最后,我的功能是:
因为我的目录语言“i18n”在项目的根目录上
相关问题 更多 >
编程相关推荐