Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

localization - How to localize a name from an external constants file in Python?

Generally speaking, I have static string that needs to be translatable.

This works ok:

# part of a wxPython program
import gettext
import locale
...
print(_("some name"))
...

Then, using xgettext, I generate the .pot catalog file, which I use further to translate the specific language .po file. Ok.

Now, I want to separate the static string into an external file. This works ok for the program itself, but I don't know how to generate the .pot file so it contains the string from the external file:

# part of a wxPython program
import gettext
import locale
from lib import external_file as ExternalFile
...
print(_(ExternalFile.SOME_NAME))
...

and

# part of external_file.py located in lib subdirectory
...
SOME_NAME = "some name"
...

The xgettext utility will only take a _ prefixed sting, but in the external file there is none. I can put the corresponding msgid string manually into the .pot file (and so the translation works well then), but this is not the proper way I would like to go.

I tried to move the underscore into the external file, but with the external file 'as is' I get "NameError: name '_' is not defined". Perhaps I need to import something there (?), but I don't know what (a simple import gettext has no effect).

Is there a way to be able to obtain a localized string on the external file ? (other than manually editing the .pot file)

question from:https://stackoverflow.com/questions/65648086/how-to-localize-a-name-from-an-external-constants-file-in-python

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...