Package: z3c.language.negotiator
This package provides a persistent INegotiator utility implementation.Detailed DocumentationNegotiatorThis package provides a local implementation of theINegotiatorinterface
defined inzope.i18n.interfaces. The negotiator implementation offers some
additional usefull attributes which are explained later. ThisINegotiatoris also used in thez3c.language.switchpackage.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')Register the negotiator as a global utility:>>> import zope.component
>>> zope.component.provideUtility(negotiator, INegotiator)Now 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'Seetests.pyfor more tests.CHANGES1.1.3 (2010-10-01)Cache ONLY for thesessionsetting. Others turned out as a bad idea.
It’s pretty straightforward now, we’re caching session data lookups because
those lookups are sooo slow.
Would be better done in z3c.language.session, but there’s nothing persistent.1.1.2 (2010-08-05)Fixed an issue with the cache:
The cache just ignored thelanguagesparameter.
The first request saved a specific language (e.g.de-de) to the cache,
but the second did not have that available inlanguages, still the cached
value was returned.
Worst case the uncached method is called.1.1.1 (2010-07-21)added language choice caching, is by default turned offtest coverage is now 100%1.1.0 (2009-11-29)adjust dependencies, reflect changes in zope packagesclanup tests, remove unused tests and skip using zope.app.testing1.0.1 (2008-01-24)Bug: Corrected meta data.1.0.0 (2008-01-21)Restructure: Removed the two vocabulariesAvailableTranslationDomainLanguagesVocabularyandAvailableTranslationDomainLanguagesVocabularyForZ3Cwhich depend on thezope.app.i18nimplementation. 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 tozope.app.i18n.Restructure: Movedzope.language.negotiatorto it’s namespace package.