Skip to main content

Zope3 i18n language negotiator.

Project description

This package provides a persistent INegotiator utility implementation.

Detailed Documentation

Negotiator

This package provides a local implementation of the INegotiator interface defined in zope.i18n.interfaces. The negotiator implementation offers some additional usefull attributes which are explained later. This INegotiator is also used in the z3c.language.switch package.

Let’s setup a negotiator:

>>> from z3c.language.negotiator import app
>>> negotiator = app.Negotiator()

Such a negotiator provides the following interfaces:

>>> from zope.i18n.interfaces import INegotiator
>>> from z3c.language.negotiator.interfaces import INegotiatorManager
>>> INegotiator.providedBy(negotiator)
True
>>>
>>> INegotiatorManager.providedBy(negotiator)
True
>>>

By default a negotiator has the following values:

>>> negotiator.policy
'session --> browser --> server'
>>> negotiator.serverLanguage
u'en'
>>> negotiator.offeredLanguages
[]

If we set a policy with a wrong value, we will get a ValueError:

>>> negotiator.policy = u'wrong'
Traceback (most recent call last):
...
ValueError: ('Not a valid policy name.', u'wrong')

Let’s add the negotiator to the site root:

>>> rootFolder['negotiator'] = negotiator

And register the negotiator as a utility:

>>> import zope.component
>>> sitemanager = zope.component.getSiteManager(rootFolder)
>>> sitemanager.registerUtility(negotiator, INegotiator)

After register the negotiator as a utility, we can use the vocabulary and see what offered languages are available:

>>> from z3c.language.negotiator import vocabulary
>>> vocab = vocabulary.OfferedLanguagesVocabulary(None)
>>> vocab
<z3c.language.negotiator.vocabulary.OfferedLanguagesVocabulary object at ...>
>>> vocab._terms
[]

Add some offered languages and check the vocabulary again:

>>> negotiator.offeredLanguages = [u'de', u'fr']
>>> negotiator.offeredLanguages
[u'de', u'fr']

Try to get the utility and ceck the offeredLanguages again:

>>> util = zope.component.getUtility(INegotiator)
>>> util.offeredLanguages
[u'de', u'fr']

Now check the vocabulary again:

>>> vocab = vocabulary.OfferedLanguagesVocabulary(None)
>>> vocab._terms[0].value
u'de'
>>> vocab._terms[0].token
'de'
>>> vocab._terms[0].title
u'de'
>>> vocab._terms[1].value
u'fr'
>>> vocab._terms[1].token
'fr'
>>> vocab._terms[1].title
u'fr'

See tests.py for more tests.

CHANGES

1.0.2 (unreleased)

  • backport from 1.1.1:

    • added language choice caching, is by default turned off

    • test coverage is now 100%

1.0.1 (2008-01-24)

  • Bug: Corrected meta data.

1.0.0 (2008-01-21)

  • Restructure: Removed the two vocabularies AvailableTranslationDomainLanguagesVocabulary and AvailableTranslationDomainLanguagesVocabularyForZ3C which depend on the zope.app.i18n implementation. Note these vocabularies are not persistent and if you will need it, let’s implement this in another package. This allows us to skip all dependencies to zope.app.i18n.

  • Restructure: Moved zope.language.negotiator to it’s namespace package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

z3c.language.negotiator-1.0.2.tar.gz (11.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page