Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Added language package
Browse files Browse the repository at this point in the history
The package contains a local negotiator,
some base classes with a i18n switch adapter
and a library for session handling support.

This packages allows you to implement simply
i18n aware content types and configure the 
server (negotiator) for handling language 
policies. Such a policy could be, that the server 
will lookup for a fix language or language settings
set on a session or the setting from the browser.

See for more information inthe different README.txt 
files or in the included tests
  • Loading branch information
projekt01 committed Aug 21, 2006
1 parent 5a99f84 commit 54fa3e1
Show file tree
Hide file tree
Showing 45 changed files with 2,548 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/z3c/language/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id$
"""

77 changes: 77 additions & 0 deletions src/z3c/language/negotiator/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
==========
Negotiator
==========

Setup testbrowser for Negotiator functional tests.

>>> from zope.testbrowser.testing import Browser
>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
>>> browser.handleErrors = False

Start Zope3 an go to the root.

>>> browser.open('http://localhost/@@contents.html')
>>> browser.url
'http://localhost/@@contents.html'

Add a negotiator.

>>> browser.open('http://localhost/++etc++site/default/@@contents.html?type_name=BrowserAdd__z3c.language.negotiator.app.Negotiator')
>>> browser.url
'http://localhost/++etc++site/default/@@contents.html?type_name=BrowserAdd__z3c.language.negotiator.app.Negotiator'
>>> browser.getControl(name='new_value').value = ''
>>> browser.getControl('Apply').click()
>>> browser.url
'http://localhost/++etc++site/default/Negotiator/@@registration.html'

And register the added negotiator utility.

>>> browser.handleErrors = False
>>> "This object isn't yet registered." in browser.contents
True
>>> browser.getLink('Registration').click()
>>> browser.getControl('Register this object').click()
>>> browser.url
'http://localhost/++etc++site/default/Negotiator/@@addRegistration.html'
>>> browser.getControl(
... 'Provided interface').value = ['zope.i18n.interfaces.INegotiator']
>>> browser.getControl('Register As').value = ''
>>> browser.getControl('Comment').value = 'A local negotiator'
>>> browser.getControl('Register', index=1).click()
>>> browser.url
'http://localhost/++etc++site/default/Negotiator/@@registration.html'

Now we see the edit form of the added and registred negotiator instance called
Negotiator. Set the language lookup policy to 'server'.

>>> browser.open('http://localhost/++etc++site/default/Negotiator/'
... '@@edit.html')
>>> browser.getControl(name='field.policy').value = ['server']

and set a default server language called 'de'.

>>> browser.getControl(name='field.serverLanguage').value = 'de'

and save this part.

>>> browser.getControl(name='UPDATE_SUBMIT').click()

Now we add a session language via the used sequence list widget.

>>> browser.getControl(name='field.sessionLanguages.add').click()
>>> browser.getControl(name='field.sessionLanguages.0.').value = 'fr'

And we also add a offered language.

>>> browser.getControl(name='field.offeredLanguages.add').click()
>>> browser.getControl(name='field.offeredLanguages.0.').value = 'de'

And as last, we save the changes.

>>> browser.getControl(name='UPDATE_SUBMIT').click()

Now check if the set language is german. See the label at the submit button.

>>> browser.getControl(name='UPDATE_SUBMIT').value
'Abschicken'
3 changes: 3 additions & 0 deletions src/z3c/language/negotiator/SETUP.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<data-files zopeskel/etc/package-includes>
z3c.language.negotiator-*.zcml
</data-files>
21 changes: 21 additions & 0 deletions src/z3c/language/negotiator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""
$Id$
"""

__docformat__ = 'restructuredtext'

from z3c.language.negotiator.interfaces import *
139 changes: 139 additions & 0 deletions src/z3c/language/negotiator/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""
$Id$
"""
__docformat__ = 'restructuredtext'

from persistent import Persistent
from zope.interface import implements

from zope.i18n.interfaces import IUserPreferredLanguages
from zope.i18n.interfaces import INegotiator
from zope.i18n.negotiator import negotiator

from zope.app.container.contained import Contained

from z3c.language.session.interfaces import ILanguageSession

from z3c.language.negotiator.interfaces import INegotiatorManager
from z3c.language.negotiator.interfaces import language_policies



class Negotiator(Persistent, Contained):
"""Loacal negotiator implementation.
The negotiator let you change the policy, which is a alias
for the lookup mechanism.
'server' -- The server defines the language
'session' -- The session defines the language (usefull for testing)
'browser' -- The browser defines the language (usefull for testing)
'browser --> session --> server' -- Left criteria first
'browser --> server' -- Left criteria first
'session --> browser --> server' -- Left criteria first (default use case)
'session --> server' -- Left criteria first
The 'server' criteria is used as a fallback criteria in most use cases.
"""

implements(INegotiator, INegotiatorManager)

def __init__(self):
self._policy = 'session --> browser --> server'
self._serverLanguage = None
self._sessionLanguages = []
self._offeredLanguages = []

def _getLanguagePolicy(self):
"""Returns the language policy."""
return self._policy

def _setLanguagePolicy(self, policy):
"""Set the language policy."""
if policy not in language_policies:
policies = str(language_policies)
msg = "Only %s are valide policy names." % policies
raise ValueError(msg, policy)
self._policy = policy

policy = property(_getLanguagePolicy, _setLanguagePolicy)

def _getServerLanguage(self):
"""Returns the language for server policy."""
return self._serverLanguage

def _setServerLanguage(self, language):
"""Set the language for server policy."""
self._serverLanguage = language

serverLanguage = property(_getServerLanguage, _setServerLanguage)

def _getSessionLanguages(self):
"""Returns the language for server policy."""
return self._sessionLanguages

def _setSessionLanguages(self, languages):
"""Set the language for server policy."""
self._sessionLanguages = languages

sessionLanguages = property(_getSessionLanguages, _setSessionLanguages)

# TODO:
# perhaps we can make a relation to the translation domains
# and dymanicly find out what language we support in the domains.
def _getOfferedLanguages(self):
"""Returns the language for server policy."""
return self._offeredLanguages

def _setOfferedLanguages(self, languages):
"""Set the language for server policy."""
self._offeredLanguages = languages

offeredLanguages = property(_getOfferedLanguages, _setOfferedLanguages)

def getLanguage(self, languages, request):
"""Returns the language dependent on the policy."""
policyList = self._policy.split(' --> ')

for policy in policyList:

# the server is handling the language
if policy == 'server':
if self.serverLanguage:
return self.serverLanguage

# the language is handled by a session
elif policy == 'session':
session = ILanguageSession(request)
lang = session.getLanguage()
if lang != None:
return lang

# the language is handled by the browsers language settings
elif policy == 'browser':
lang = negotiator.getLanguage(languages, request)
if lang != None:
return lang

return None
17 changes: 17 additions & 0 deletions src/z3c/language/negotiator/browser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""
$Id$
"""
39 changes: 39 additions & 0 deletions src/z3c/language/negotiator/browser/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns="http://namespaces.zope.org/browser"
i18n_domain="z3c.language">

<!-- local negotiator -->
<addMenuItem
title="Negotiator"
description="Negotiator returns the i18n language for translation."
class="z3c.language.negotiator.app.Negotiator"
permission="zope.ManageSite"
/>

<editform
name="edit.html"
label="Negotiator for i18n language lookup."
schema="..INegotiatorManager"
menu="zmi_views" title="Edit"
permission="zope.ManageContent"
/>

<!-- negotiator views -->
<page
for="*"
name="offered_languages"
permission="zope.Public"
class=".views.NegotiatorView"
attribute="getOfferedLanguages"
/>

<page
for="*"
name="hasOfferedLanguages"
permission="zope.Public"
class=".views.NegotiatorView"
attribute="hasOfferedLanguages"
/>

</configure>
59 changes: 59 additions & 0 deletions src/z3c/language/negotiator/browser/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""
$Id$
"""
__docformat__ = 'restructuredtext'

from zope.interface import implements
from zope.i18n.interfaces import INegotiator
from zope.publisher.browser import BrowserView

from zope.app.zapi import getUtility

from z3c.language.negotiator import IOfferedLanguages



class NegotiatorView(BrowserView):

implements(IOfferedLanguages)

def getOfferedLanguages(self):
"""View for listing available (offered) languages."""

negotiator = getUtility(INegotiator, '', self.context)

try:
offeredLanguages = negotiator.offeredLanguages
except:
# we don't have a Negotiator instance
# we got the global zope.i18n Negotiator
offeredLanguages = []

return offeredLanguages

def hasOfferedLanguages(self):
"""View for to check if we have i18n session support."""

negotiator = getUtility(INegotiator, '', self.context)

try:
offeredLanguages = negotiator.offeredLanguages
return True
except:
# we don't have a Negotiator instance
# we got the global zope.i18n Negotiator
return False
Loading

0 comments on commit 54fa3e1

Please sign in to comment.