Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - pygettext doesn't read strings into the .pot file

I try to make a multilingual application with pygettext. I reproduce the steps described here: https://phrase.com/blog/posts/translate-python-gnu-gettext/.

The translatable strings should appear at the bottom of the .pot file:

#: src/main.py:5
msgid "Hello world"
msgstr ""
#: src/main.py:6
msgid "This is a translatable string"
msgstr ""

The problem is that in my case there msgid are empty, though .pot file is created. It looks like pygettext just doesn't see them in the code.

That's how my whole .pot file looks like:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION
"
"POT-Creation-Date: 2021-01-16 13:50+0300
"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>
"
"Language-Team: LANGUAGE <LL@li.org>
"
"MIME-Version: 1.0
"
"Content-Type: text/plain; charset=cp1251
"
"Content-Transfer-Encoding: 8bit
"
"Generated-By: pygettext.py 1.5
"

My python code:

# main.py
import gettext
_ = gettext.gettext


def print_some_strings():
    print(_("Hello world"))
    print(_("This is a translatable string"))


if __name__ == '__main__':
    print_some_strings()

Project structure:

pdf_invoices
│ └── en
│     └── LC_MESSAGES
│         └── base.pot
└──__main__.py

Command creating .pot file:

C:>C:UserseloseAnaconda3Toolsi18npygettext.py 
-d C:UserselosePycharmProjectspdf_invoices\__main__.py 
-o C:UserselosePycharmProjectspdf_invoiceslocales
uase.pot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You run pygettext in the command line. But you should run python first. In your case:

    C:>python C:UserseloseAnaconda3Toolsi18npygettext.py
   -d C:UserselosePycharmProjectspdf_invoices\__main__.py 
   -o C:UserselosePycharmProjectspdf_invoiceslocales
uase.pot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...