You may want to sort out error messages depending on the locale. I didn't know what to do in such a case, so I looked it up.
translationstring
Use translationstring. This seems to be used for localization in the package related to the pylons project. It seems that it uses the gettext mechanism that is often used for general translation.
"Failure" in Japanese locale. Otherwise, consider a string that displays "failure".
It seems that the function generated by translationstring.TranslationStringFactory should be used for the notation. Wrap it in a Translator Object to convert the value.
In addition to the code below, you need a .mo file in place.
translationstring_example.py
# -*- coding:utf-8 -*-
import gettext
import translationstring
# .To work well with the po file generation tool_It is better to name it
_ = translationstring.TranslationStringFactory('sample')
lang = gettext.translation("sample", "./locale", languages=["ja"], codeset="utf8")
translator = translationstring.Translator(lang)
print(translator(_("failure")))
The .mo file is the format used by gettext. Generated from a .po file using commands using msgfmt. Generate a .po file before the .mo file. Use pot-create for this.
A package called lingua will generate a .po file for you. ligua itself doesn't need babel, but babel was requested when trying to run pot-create.
pip install lingua babel
After installing lingua, you can use pot-create. Generate a .po file in place
mkdir -p locale/ja/LC_MESSAGES/
pot-create translation_example.py -o locale/ja/LC_MESSAGES/sample.po
The file name to rewrite the generated sample.po should be the same as the domain name passed to TranslationStringFactory.
Rewrite as follows. Originally, it is better to rewrite the part such as the translator's name. This time I just rewrote the last part
#
# SOME DESCRIPTIVE TITLE
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2014.
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"POT-Creation-Date: 2014-06-12 15:37+09:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: ./translation_example.py:11
#, c-format
msgid "failure"
msgstr "Failure value"
The difference is as follows
diff --git a/locale/ja/LC_MESSAGES/sample.po b/locale/ja/LC_MESSAGES/sample.po
index 2559ffb..db040c4 100644
--- a/locale/ja/LC_MESSAGES/sample.po
+++ b/locale/ja/LC_MESSAGES/sample.po
@@ -18,4 +18,4 @@ msgstr ""
#: ./translation_example.py:11
#, c-format
msgid "failure"
-msgstr ""
+msgstr "Failure value"
Use msgfmt to create .mo files.
msgfmt locale/ja/LC_MESSAGES/sample.po -o locale/ja/LC_MESSAGES/sample.mo
$ tree
.
├── locale
│ └── ja
│ └── LC_MESSAGES
│ ├── sample.mo
│ └── sample.po
├── requirements.txt
└── translation_example.py
3 directories, 4 files
This will translate "failure" into "failure value" and display it.
$ python translation_example.py
Failure value
gettext looks at the locales information and asks for the mapping to use when translating.
LANG="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_CTYPE="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_ALL=
It seems good to use gettext.find to check if the .mo file can be read successfully.
# -*- coding:utf-8 -*-
import gettext
print(gettext.find("sample", "./locale",["ja"]))
# ./locale/ja/LC_MESSAGES/sample.mo
You can find out where to create the .po file by looking at the gettext.find code.
gettext.py
# Locate a .mo file using the gettext strategy
def find(domain, localedir=None, languages=None, all=False):
# Get some reasonable defaults for arguments that were not supplied
if localedir is None:
localedir = _default_localedir
if languages is None:
languages = []
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
val = os.environ.get(envar)
if val:
languages = val.split(':')
break
if 'C' not in languages:
languages.append('C')
# now normalize and expand the languages
nelangs = []
for lang in languages:
for nelang in _expand_lang(lang):
if nelang not in nelangs:
nelangs.append(nelang)
# select a language
if all:
result = []
else:
result = None
for lang in nelangs:
if lang == 'C':
break
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
if os.path.exists(mofile):
if all:
result.append(mofile)
else:
return mofile
return result
Roughly, get languages such as "ja" from ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'), You can see that it is trying to retrieve the value from the .mo file under localedir / ja / LC_MESSAGS /.
translationstring can also translate sentences that take arguments.
msg2 = _("add-number", default="Add ${number}", mapping={"number": 1})
lang = gettext.translation("sample", "./locale", languages=["ja"], codeset="utf8")
translator = translationstring.Translator()
print(translator(_("add-number", mapping={"number": 10})))
#Add 10
At this time, write the .placeholder in $ {} in the .po file as shown below.
msgid "add-number"
msgstr "${number}Please add"
Recommended Posts