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

2024-10-02 02:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是使用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。在

以下是我的文件和目录结构:

enter image description here

接下来是我的档案内容。。。在

多布尔斯_消息.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”选项,得到以下错误:

enter image description here

在我的第一步中,我定义了域“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。我什么都不懂:(


Tags: 文件thepackagemessagedateversioncontentlanguage
2条回答

Question: using gettext library to translate text


根据文件:

gettext(message)

Look up the message id in the catalog and return the corresponding message string, as a Unicode string. If there is no entry in the catalog for the message id, and a fallback has been set, the look up is forwarded to the fallback’s gettext() method. Otherwise, the message id is returned.


如果您更改以下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')

According to Python Documentation, you have to do:

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

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

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”在项目的根目录上

相关问题 更多 >

    热门问题