Python:翻译文本显示msgid而不是msgstr

时间:2018-10-10 14:11:27

标签: python python-3.x gettext

我是使用gettext库翻译文本的新手。我要翻译下一个文件中的文本:

dobles_message.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")

要做到这一点,首先我要使用以下说明创建dobles_message.po:

xgettext -i dobles_message.py -o dobles_message.pot -d basketmetrics_i18n

然后,我创建了以下目录结构i18n / es / LC_MESSAGES和i18n / en / LC_MESSAGES,并按照以下说明为每种语言创建了.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。

这些是我的文件和目录结构:

enter image description here

接下来是我文件的内容...

dobles_message.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

编辑我:

在我的代码中,我删除了“ fallback = True”选项,并且出现此错误:

enter image description here

第一步,我定义域“ basmetmetrics_i18n”,并使用该名称创建了.po文件。发生了什么事?

编辑II:

按照@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。我什么都不懂:(

2 个答案:

答案 0 :(得分:0)

  

问题:使用gettext库翻译文本


从文档中:

  

获取文本(消息)

     

在目录中查找消息ID,并以Unicode字符串的形式返回相应的消息字符串。 如果目录ID中没有邮件ID的条目,并且已设置后备,则查找将转发到后备的gettext()方法。 否则,将返回邮件ID


如果您更改以下languages=[language],您的代码将对我有用:

def set_message(self, language):
    t = gettext.translation(package, localedir, fallback=True, languages=[language])
    t.install()
    _ = t.gettext
    message = _("mensaje_dd_1")

set_message('es')

  

根据Python Documentation,您必须执行以下操作:

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print(_('This is a translatable string.'))

答案 1 :(得分:0)

最后,我找到了错误。由于语言目录所在目录的路径而导致的错误。您必须从项目的根目录而不是您要翻译文本的文件中指定此路由。所以,我的功能终于是:

def set_message(self, language):
    t = gettext.translation("basketmetrics_i18n", "../i18n", fallback=True, languages=["'" + language + "'"])
    t.install()
    _ = t.gettext
    self.message = _("mensaje_dd_1")

因为我的目录语言“ i18n”位于项目的根目录上