1.gettext Module Introduction
The GetText module provides internationalization (i18n) and localization (l10n) services for Python modules and applications. It offers two sets of APIs, a set of high-level APIs similar to the GNU GetText and a set of class-based APIs. The former applies to single-language scenarios, and the choice of language usually relies on the user locale, which affects the translation of your entire application language globally. The latter allows you to localize within a Python module and is ideal for switching your language while the application is running.
2. General steps
(1) regardless of which set of APIs to use, you must extract the translatable strings and generate a translation template file. pot
(2) then generate a file corresponding to the translation in different languages. PO
(3) We use the binary format. Mo file in the application, which is converted from. po
Now give a simple example to illustrate the above steps
(a) Create a project catalog py_i18n, creating a program master file my_app.py
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B2/wKioL1WDhE3CLn17AADJcWAjfXI010.jpg "title=" g1.png "alt=" Wkiol1wdhe3cln17aadjcwajfxi010.jpg "/>
(b) Edit my_app.py, first enter the following content
#-*-coding:utf-8-*-import gettext_ = gettext.gettextif __name__ = = "__main__": Print _ ("Hello World")
The string enclosed in _ () is a string that needs to be translated.
(c) Use Xgetext to extract the translated string to generate the translation template
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/B6/wKiom1WDguHzRci0AADH-xP3uNM159.jpg "title=" g2.png "alt=" Wkiom1wdguhzrci0aadh-xp3unm159.jpg "/>(d) uses Msginit to generate files corresponding to translations in different languages. Po, such as Simplified Chinese zh_cn and English en:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B6/wKiom1WDgz6gdpv-AALr-syQif8893.jpg "title=" g3.png "alt=" Wkiom1wdgz6gdpv-aalr-syqif8893.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B6/wKiom1WDhtHS6KrMAALkxrIkWik590.jpg "title=" g4.png "alt=" wkiom1wdhths6krmaalkxrikwik590.jpg "/> Notice process, you may ask your email address, directly enter the
(e) Edit the Po file and fill in the translation string corresponding to the string that needs to be translated, for example Zh_cn.po:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/B2/wKioL1WDicqihPXtAAMHbJqjuSU263.jpg "title=" G5.png "alt=" wkiol1wdicqihpxtaamhbjqjusu263.jpg "/> the string behind this msgid is the string that needs to be translated, and Msgstr is followed by the target string. Repeat (e) operation on En.po
(f) Generate binary files from PO using the MSGFMT command, which is also the file that the application needs to use.
Create the locale directory for the MO file in the project directory, the same directory structure as the system's locale directory:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B5/wKioL1WDwoWyf5RbAACi_Xq8OCY963.jpg "title=" g6.png "alt=" Wkiol1wdwowyf5rbaaci_xq8ocy963.jpg "/>
Then generate a different MO file in the corresponding directory
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B9/wKiom1WDyU2B_qPiAADTVvh4PeM059.jpg "title=" g7.png "alt=" Wkiom1wdyu2b_qpiaadtvvh4pem059.jpg "/>
Ok! So far all the preparatory work has been done
Add:
Note: The above steps can also be done with poedit This software, the official website has a detailed use of the tutorial
In addition, once the string to be translated in Python code changes (modified, added or deleted), you can use the msgmerge command instead of the msginit command when you need to regenerate the Po file, for example:
Msgmerge-u Zh_cn.po My_app.pot
3. Add internationalization support in Python code
in the first part of the introduction of the GetText module, the GetText module is used in two ways. The first is based on the user's system language configuration (System environment variables LANGUAGE, lc_all, lc_messages, LANG), automatically select the language The second is to switch languages in real time in the program.
(1) automatic language selection according to the language configuration of the user system
Open my_app.py, edit the code and Save:
#-*-Coding:utf-8-*-import osimport gettextapp_name = "My_app" Locale_dir = Os.path.abspath ("LOCALE") # Place domain App_name with local E_dir directory binding, # so the GetText function searches the Locale_dir directory for the binary App_name.mo file of the corresponding language Gettext.bindtextdomain (App_name, Locale_dir) # Declare using the current domain, you can use more than one domain, you can provide multiple sets of translations for the same language, # This program only uses a domain Gettext.textdomain (app_name) _ = gettext.gettextif __name__ = = "__main__ ": Print _ (" Hello World ")
Then go back to the command-line window, check the current language configuration, and run the Python my_app.py:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B6/wKioL1WD1HzCdiBwAAC21atLpyA238.jpg "title=" g8.png "alt=" Wkiol1wd1hzcdibwaac21atlpya238.jpg "/>
OK, get the correct translation output!
We can try to switch languages and modify the system's environment variables directly:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/BA/wKiom1WD0yrTOrXiAADT7OEFNgk237.jpg "title=" g9.png "alt=" Wkiom1wd0yrtorxiaadt7oefngk237.jpg "/>
We can see that we have not modified the code, but the program has automatically output English
(2) switching languages in real time in the program
Open my_app.py, edit the code and Save:
#-*-Coding:utf-8-*-import osimport gettextapp_name = "My_app" Locale_dir = Os.path.abspath ("LOCALE") # This statement automatically puts the _ () function to P Ython built-in namespace Gettext.install (App_name, Locale_dir) # Get an example of a simplified Chinese translation class lang_zh_cn = Gettext.translation (App_name, LOCALE_ DIR, ["ZH_CN"]) # Get an example of an English translation class lang_en = Gettext.translation (App_name, Locale_dir, ["en"]) if __name__ = = "__main__": # Install Chinese Lang_zh_cn.install () Print _ ("Hello World") # Install English (program in real-time switch back to English) Lang_en.install () Print _ ("Hello World")
Go back to the command line and run the Python my_app.py directly, running the following result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/BA/wKiom1WD1hfw25zDAAB3Lm6gPAM378.jpg "title=" G10.png "alt=" Wkiom1wd1hfw25zdaab3lm6gpam378.jpg "/>
As you can see, you can output both Chinese and English in the program.
So far, two ways to add internationalization support to Python are complete.
This article from "Underthehood" blog, declined reprint!
GetText module for Python internationalization support