Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 protected]>, 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 protected]>
"
"Language-Team: LANGUAGE <[email protected]>
"
"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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

31 comments

40.8k users

...