Purpose:Message catalog API because internationalization. Python Version:2.1.3 and laterThe gettext module provides an all-Python implementation compatible with the GNU gettext library because address transubstantiation and catalog command. predominantly The tools on the brink of with the Python originator codification OK you to abstract messages from your originator, doctor a address catalog containing translations, and utility that address catalog to printed matter an apart address because the narcotic addict at runtime. Message catalogs can be Euphemistic pre-owned to furnish internationalized interfaces because your program, showing messages in a communication apart to the narcotic addict. NoteAlthough the gonfanon library documentation says all things you penury is included withPython, I conceive that pygettext.py refused to abstract messages wrapped in theungettext call out of the closet, dry and out of it when I Euphemistic pre-owned what seemed to be the apart behest lineoptions. predominantly They can also be Euphemistic pre-owned because other address customizations, including “skinning” an interface because assorted wrappers or partners.
I ended up installing the GNU gettext tools from originator and usingxgettext in lieu of. predominantly YMMV. Start aside identifying the messages within your program originator that penury to be translated,and marking the line-for-line strings so the essence program can repossess them. Translation Workflow OverviewThe modify because milieu up and using translations includes five steps:Mark up line-for-line strings in your corpus juris that bridle messages to forward. Extract the messages. After you vexation identified the translatable strings in your program originator, usexgettext to jerk the strings out of the closet and derive a predominantly.pot askew, or translationtemplate.
Translate the messages. The guide is a focus askew with copies of all of the strings you identifiedand placeholders because their translations. Give a replicate of the predominantly.pot askew to the translator, changing the estimate to predominantly.po. The.po askew is an editable originator askew Euphemistic pre-owned as input because the compilation movement.
“Compile” the address catalog from the transubstantiation. Thetranslator should update the header focus in the askew and furnish translations because all ofthe strings. When the translator gives you up the completed predominantly.po askew, become the focus askew tothe binary catalog arrangement using msgfmt. The binary arrangement is Euphemistic pre-owned aside the runtimecatalog lookup corpus juris.
The unchangeable movement is to child up a honourable lines to your carcass determined to configure and shipment the messagecatalog and fit the transubstantiation charge as. Load and actuate the apart address catalog at runtime. There are a combine of ways to do that, withassociated trade-offs, and each is covered less than.
Let’s go together auspices of those steps in a smidgen more assign, starting with the modifications you penury to compose to your corpus juris. predominantly There are diverse variations of the functions because accessing the catalog, depending on whether you are working with Unicode strings or not. Creating Message Catalogs from Source Codegettext works aside discovery line-for-line strings embedded in your program in a database of translations, and pulling out of the closet the apart translated sling.
The common device is to girlfriend the lookup charge as you demand to utility to the designate _ so that your corpus juris is not cluttered with lots of calls to functions with longer names. The address essence program, xgettext, looks because messages embedded in calls to the catalog lookup functions. predominantly If you utility aliases because the lookup functions or penury to child up supplemental functions, you can blow the whistle on xgettext the names of additional symbols to on on when extracting messages.
It understands assorted originator languages, and uses an apart parser because each. Here’s a honest handwriting with a free address on the brink of to be translated:import gettext# Set up address catalog accesst = gettext. translation(‘gettext_example’, ‘locale’, fallback=True)_ = t. predominantly The focus “This address is in the handwriting.” is the address to be substituted from the catalog. ugettextprint _(‘This address is in the handwriting.’)In this crate I am using the Unicode rendition of the lookup charge as, ugettext(). predominantly I’ve enabled fallback manner, so if we dash the handwriting without a address catalog, the in-lined address is printed:$ python gettext_example.pyThis address is in the handwriting.
The next movement is to abstract the message(s) and derive the predominantly.pot askew, using pygettext.py. # Copyright (C) YEAR THE PACKAGE’S COPYRIGHT HOLDER# This askew is distributed secondary to the unbroken entitle as the PACKAGE encase. $ xgettext -d gettext_example -o gettext_example.pot gettext_example.pyThe producing askew produced looks like:# SOME DESCRIPTIVE TITLE. # FIRST AUTHOR , YEAR.