ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 02.01.2026
Просмотров: 2709
Скачиваний: 0
21. INTERNATIONALIZATION AND LOCALIZATION
$ cd /my/virtualenv
$ bin/easy_install Babel lingua
Installation on Windows
If the virtualenv into which you’ve installed your Pyramid application lives in C:\my\virtualenv, you can install Babel and Lingua like so:
C> cd \my\virtualenv
C> Scripts\easy_install Babel lingua
Changing the setup.py
You need to add a few boilerplate lines to your application’s setup.py file in order to properly generate gettext files from your application.
latex-note.png
See Creating a Pyramid Project to learn about about the composition of an application’s setup.py file.
In particular, add the Babel and lingua distributions to the install_requires list and insert a set of references to Babel message extractors within the call to setuptools.setup() inside your application’s setup.py file:
1setup(name="mypackage",
2# ...
3install_requires = [
4 |
# ... |
5 |
’Babel’, |
6 |
’lingua’, |
7 |
], |
8message_extractors = { ’.’: [
9 |
(’**.py’, ’lingua_python’, None ), |
234
21.2. WORKING WITH GETTEXT TRANSLATION FILES
10 |
(’**.pt’, ’lingua_xml’, None ), |
11 |
]}, |
12 |
) |
|
|
The message_extractors stanza placed into the setup.py file causes the Babel message catalog extraction machinery to also consider *.pt files when doing message id extraction.
21.2.2 Extracting Messages from Code and Templates
Once Babel and Lingua are installed and your application’s setup.py file has the correct message extractor references, you may extract a message catalog template from the code and Chameleon templates which reside in your Pyramid application. You run a setup.py command to extract the messages:
$ cd /place/where/myapplication/setup.py/lives $ mkdir -p myapplication/locale
$ $myvenv/bin/python setup.py extract_messages
The message catalog .pot template will end up in:
myapplication/locale/myapplication.pot.
Translation Domains
The name myapplication above in the filename myapplication.pot denotes the translation domain of the translations that must be performed to localize your application. By default, the translation domain is the project name of your Pyramid application.
To change the translation domain of the extracted messages in your project, edit the setup.cfg file of your application, The default setup.cfg file of a pcreate -generated Pyramid application has stanzas in it that look something like the following:
1
2
3
4
5
6
7
8
[compile_catalog]
directory = myproject/locale domain = MyProject statistics = true
[extract_messages] add_comments = TRANSLATORS:
output_file = myproject/locale/MyProject.pot
235
21. INTERNATIONALIZATION AND LOCALIZATION
9width = 80
10
11[init_catalog]
12domain = MyProject
13input_file = myproject/locale/MyProject.pot
14output_dir = myproject/locale
15
16[update_catalog]
17domain = MyProject
18input_file = myproject/locale/MyProject.pot
19output_dir = myproject/locale
20previous = true
In the above example, the project name is MyProject. To indicate that you’d like the domain of your translations to be mydomain instead, change the setup.cfg file stanzas to look like so:
1[compile_catalog]
2 directory = myproject/locale
3 domain = mydomain
4statistics = true
5
6[extract_messages]
7add_comments = TRANSLATORS:
8 output_file = myproject/locale/mydomain.pot
9width = 80
10
11[init_catalog]
12domain = mydomain
13input_file = myproject/locale/mydomain.pot
14output_dir = myproject/locale
15
16[update_catalog]
17domain = mydomain
18input_file = myproject/locale/mydomain.pot
19output_dir = myproject/locale
20previous = true
21.2.3 Initializing a Message Catalog File
Once you’ve extracted messages into a .pot file (see Extracting Messages from Code and Templates), to begin localizing the messages present in the .pot file, you need to generate at least one .po file. A
.po file represents translations of a particular set of messages to a particular locale. Initialize a .po file
236
21.2. WORKING WITH GETTEXT TRANSLATION FILES
for a specific locale from a pre-generated .pot template by using the setup.py init_catalog command:
$ cd /place/where/myapplication/setup.py/lives
$ $myvenv/bin/python setup.py init_catalog -l es
By default, the message catalog .po file will end up in:
myapplication/locale/es/LC_MESSAGES/myapplication.po.
Once the file is there, it can be worked on by a human translator. One tool which may help with this is Poedit.
Note that Pyramid itself ignores the existence of all .po files. For a running application to have translations available, a .mo file must exist. See Compiling a Message Catalog File.
21.2.4 Updating a Catalog File
If more translation strings are added to your application, or translation strings change, you will need to update existing .po files based on changes to the .pot file, so that the new and changed messages can also be translated or re-translated.
First, regenerate the .pot file as per Extracting Messages from Code and Templates. Then use the setup.py update_catalog command.
$ cd /place/where/myapplication/setup.py/lives $ $myvenv/bin/python setup.py update_catalog
21.2.5 Compiling a Message Catalog File
Finally, to prepare an application for performing actual runtime translations, compile .po files to .mo files:
$ cd /place/where/myapplication/setup.py/lives $ $myvenv/bin/python setup.py compile_catalog
This will create a .mo file for each .po file in your application. As long as the translation directory in which the .mo file ends up in is configured into your application, these translations will be available to Pyramid.
237
21. INTERNATIONALIZATION AND LOCALIZATION
21.3 Using a Localizer
A localizer is an object that allows you to perform translation or pluralization “by hand” in an application. You may use the pyramid.i18n.get_localizer() function to obtain a localizer. This function will return either the localizer object implied by the active locale negotiator or a default localizer object if no explicit locale negotiator is registered.
1
2
3
4
from pyramid.i18n import get_localizer
def aview(request):
locale = get_localizer(request)
latex-note.png
If you need to create a localizer for a locale use the pyramid.i18n.make_localizer() function.
21.3.1 Performing a Translation
A localizer has a translate method which accepts either a translation string or a Unicode string and which returns a Unicode object representing the translation. So, generating a translation in a view component of an application might look like so:
1 |
from pyramid.i18n import get_localizer |
2 |
from pyramid.i18n import TranslationString |
3 |
|
4 |
ts = TranslationString(’Add ${number}’, mapping={’number’:1}, |
5 |
domain=’pyramid’) |
6 |
|
7 |
def aview(request): |
8localizer = get_localizer(request)
9 translated = localizer.translate(ts) # translation string
10 # ... use translated ...
238
21.3. USING A LOCALIZER
The get_localizer() function will return a |
pyramid.i18n.Localizer |
object |
|
bound to the locale name represented by the request. |
The translation returned from its |
||
pyramid.i18n.Localizer.translate() method |
will |
depend on the domain |
attribute |
of the provided translation string as well as the locale of the localizer. |
|
||
latex-note.png
If you’re using Chameleon templates, you don’t need to pre-translate translation strings this way. See Chameleon Template Support for Translation Strings.
21.3.2 Performing a Pluralization
A localizer has a pluralize method with the following signature:
1
2
def pluralize(singular, plural, n, domain=None, mapping=None):
...
The singular and plural arguments should each be a Unicode value representing a message identifier. n should be an integer. domain should be a translation domain, and mapping should be a dictionary that is used for replacement value interpolation of the translated string. If n is plural for the current locale, pluralize will return a Unicode translation for the message id plural, otherwise it will return a Unicode translation for the message id singular.
The arguments provided as singular and/or plural may also be translation string objects, but the domain and mapping information attached to those objects is ignored.
1
2
3
4
5
6
from pyramid.i18n import get_localizer
def aview(request):
localizer = get_localizer(request)
translated = localizer.pluralize(’Item’, ’Items’, 1, ’mydomain’)
# ... use translated ...
239