Hi,

On Wed, Apr 26, 2006 at 11:14:19AM +0200, Riccardo Tonon wrote:
> Hi everybody,
> 
> I'm developing a multilanguage site in Zope 3.
> 
> I've just prepared it to support the i18n based on the browser language.
> 
> I would like also to let the user decide which language to use. (e.g. using a 
> combo 
> box present on each page of the site)
> 
> Anybody has an example where I could learn from?

I'm developing a multilingual site, too. It works like this:
   * browser language preferences are usually used to determine favorite 
language
   * users can click on one of 3 small flags to override browser preferences
     with a different language
   * all data fields on content objects are stored as dictionaries like this:
     context.sampledata={'de':'Einige Daten','en':'Some Data'}
   * Data fields are inserted into pagetemplates using a special metal-namespace
     <div tal:content="context/sampledate/mpgi18n:text />
   * The displayed language version is negotiated which means that the
     original browser language preference list is really treated as a
     list.

I attached the necessary BrowserLanguage-adapter, zcml (look for the
zope:adapter-statement) and the BrowserView which is used to define the
override language.

Disadvantage of my solution:
   * It's using (session)cookies

hth.

Frank
<?xml version="1.0" encoding="iso-8859-1"?>
<configure
        xmlns="http://namespaces.zope.org/browser";
        xmlns:zope="http://namespaces.zope.org/zope";
        xmlns:i18n="http://namespaces.zope.org/i18n";
        i18n_domain="mpgsite"
        >

        <!-- I18N Widget -->
        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IInputWidget"
                for="mpgsite.i18n.interfaces.II18N"
                factory="mpgsite.i18n.browser.widgets.SimpleI18NInputWidget"
                permission="zope.Public"
        />

        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IDisplayWidget"
                for="mpgsite.i18n.interfaces.II18N"
                factory="mpgsite.i18n.browser.widgets.I18NDisplayWidget"
                permission="zope.Public"
        />
        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IDisplayWidget"
                for="mpgsite.i18n.interfaces.II18N 
zope.schema.interfaces.ITextLine"
                factory="mpgsite.i18n.browser.widgets.I18NTextDisplayWidget"
                permission="zope.Public"
        />
        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IDisplayWidget"
                for="mpgsite.i18n.interfaces.II18N 
zope.schema.interfaces.IBytesLine"
                factory="mpgsite.i18n.browser.widgets.I18NTextDisplayWidget"
                permission="zope.Public"
        />
        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IDisplayWidget"
                for="mpgsite.i18n.interfaces.II18N 
zope.schema.interfaces.ISourceText"
                factory="mpgsite.i18n.browser.widgets.I18NTextDisplayWidget"
                permission="zope.Public"
        />
        <zope:view
                type="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.app.form.interfaces.IDisplayWidget"
                for="mpgsite.i18n.interfaces.II18N 
zope.schema.interfaces.IField"
                factory="mpgsite.i18n.browser.widgets.I18NSimpleDisplayWidget"
                permission="zope.Public"
        />

                <addform
                        name="add_I18NConfig.html"
                        schema="mpgsite.i18n.interfaces.II18NConfig"
                        content_factory="mpgsite.i18n.app.I18NConfig"
                        permission="zope.ManageContent"
                        label="Add I18N-configuration utility"
                        >
                        <widget field="i18nsortorder" 
class="zope.app.form.browser.BytesWidget"
                                required="False" convert_missing_value="False" 
/>
                </addform>
                <addMenuItem
                        class="mpgsite.i18n.app.I18NConfig"
                        title="I18N Config"
                        description="A configuration utility for I18N content"
                        permission="zope.ManageContent"
                        view="add_I18NConfig.html"
                        />

                <editform
                        schema="mpgsite.i18n.interfaces.II18NConfig"
                        name="edit.html"
                        permission="zope.ManageContent"
                        i18n:domain="zope"
                        menu="zmi_views"
                        title="Edit"
                        label="Edit"
                        />
                <defaultView
                        for="mpgsite.i18n.interfaces.II18NConfig"
                        name="edit.html"
                        />
        <tool
                interface="mpgsite.i18n.interfaces.II18NConfig"
                title="I18N Configuration"
                description="A configuration utility for I18N content"
                />

        <!-- Cookie-Language-Switcher -->
        <page
                for="*"
                name="set_preferred_language.html"
                class=".views.CookieLanguageSwitchView"
                attribute="setlanguage"
                permission="zope.View"
                layer="mpgsite.skin.mpg"
                />
        <zope:adapter
                for="zope.publisher.interfaces.browser.IBrowserRequest"
                provides="zope.i18n.interfaces.IUserPreferredLanguages"
                factory=".adapters.CookieBrowserLanguages"
                trusted="True"
                permission="zope.View"
                />
        <page
                for="*"
                name="get_preferred_language"
                class=".views.GetLanguageView"
                permission="zope.View"
                allowed_attributes="getClass"
                layer="mpgsite.skin.mpg"
        />
</configure>
from zope.app.publisher.browser import BrowserView
from mpgsite.i18n.browser.tools import getPreferredLanguages
from zope.formlib import form
from zope.interface import Interface

class CookieLanguageSwitchView(BrowserView):
        """A view that sets a new preferred language by sending a cookie to the 
user.
                Presence of this cookie overrides preferred-language settings 
in the browser.
                This is useful for systems where the user is unable to control 
the browser
                settings."""
        def setlanguage(self, language, newurl):
                response = self.request.response
                response.setCookie('mpgsite.preferredlanguage', language, 
path='/')
                response.redirect(newurl)

class GetLanguageView(BrowserView):
        """Returns the current language selection"""
        def __call__(self):
                languages=getPreferredLanguages(self.request)
                return languages[0]

        def getClass(self,language):
                if language == self.__call__():
                        return 'MpgSelectedLanguage'
                else:
                        return 'MpgUnSelectedLanguage'
                        
class I18NEditView(form.EditForm):
        schema=Interface
        def __init__(self,context,request):
                self.form_fields=form.Fields(self.schema)

        @form.action("edit")
        def handle_edit_action(self,action,data):
                if form.applyChanges(self.context,self.form_fields,data):
                        self.status='Object updated'
                else:
                        self.status='No changes'

        def __call__(self):
                self.update()
                self.i18nwidgetcontroller=I18NWidgetController(self)
                self._show_navmenu=False
                for widget in self.widgets.__Widgets_widgets_list__:
                        widget.i18nwidgetcontroller=self.i18nwidgetcontroller
                return self.index()
from zope.component import adapts
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.publisher.browser import BrowserLanguages
from zope.i18n.interfaces import IUserPreferredLanguages


class CookieBrowserLanguages(BrowserLanguages):
        adapts(IBrowserRequest)

        def getPreferredLanguages(self):
                lang = self.request.cookies.get('mpgsite.preferredlanguage', 
None)
                if lang is None:
                        
languages=super(CookieBrowserLanguages,self).getPreferredLanguages()
                        return languages
                return [lang]


_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to