Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]