Hi Everyone,

I have a 'good enough', for now, solution to my problem.

Thank you, Philipp, for dumbing down your explanation so 
that an inexperienced zope3 user can try and get some useful
work done.  Perhaps one day I will understand the traversal and
event mechanisms well enough to appreciate your suggestion.
Vinny's point about the principal's existence is how I've
decided to proceed at this point (thanks!).  

Roger, I will look to the z3c.language product in the
future.  Thanks for pointing it out.

I get to use the zope.app.preferences mechanism which
pleases me.  It'll save me the work of having to implement
yet another preference setup.


Here is the final code for those who are interested.  Watch
out for wrapping!

============== language.py =============
from zope.interface import implements
from zope.i18n.interfaces import IModifiableUserPreferredLanguages
from zope.publisher.browser import BrowserLanguages

from zope.app.preference import UserPreferences

class BrowserFormLanguages(BrowserLanguages):
    A class for customizing the setting of user language
    The preference is stored using the zope.app.preferences
    machinery.  This machinery uses principal annotations
    so a principal is required, which the 'request' may
    contain.  We have to test for it though.

    DEBUG: print statements will need to be removed at some point.


    def __init__(self, request):
        super(BrowserFormLanguages, self).__init__(request)
        print "created BFL"

    def setPreferredLanguages(self, langs):
        This function was initially needed for the ++lang++ thingy.
        I'm not sure whether I should set the UserPreferences here.
        However, if I don't set the language here then I'm not fulfilling the
        interface contract.
        I also don't really know what I am expected to set nor where.
        Couldn't find any documentation on it.  Bit of a mystery,
        although I suppose that if you've defined setPreferredLanguages you're
        expected to define storage for it.
        if self._havePrincipal():
            prefs = self._getPrefs()
            if prefs:
                # We have to make sure to catch errors thrown when values passed
                # violate a constraint. I.e. ++lang++somethingsilly
                    # We will only use the first list element.
                    # I wonder what's gonna break...
                    print "Given", langs, "to set language for", 
self.request.principal.id + '.', "Using", langs[0]
                    prefs.eods.general.language = langs[0]
                    print "Could not set language to", langs

    def _havePrincipal(self):
        Check if we have a principal that has possible preferences.
        if self.request.principal is not None:
            return True
            return False

    def _getPrefs(self):
        Return the user's preference object.
            prefs = UserPreferences()
            print "Prefs for", self.request.principal.id, 
            return prefs
            return False

    def getPreferredLanguages(self):
        # Get the browser's list of languages.
        langs = super(BrowserFormLanguages, self).getPreferredLanguages()

        # Look to see if the request has a Language attribute from a form
        # This may be used in the future.
        form_lang = self.request.get("Language", None)
        if form_lang is not None:
            langs.insert(0, form_lang)

        # look for a user stored preference which overrides a form.
        # Might want to switch the selection based on different priorities.
        if self._havePrincipal():
            prefs = self._getPrefs()
            if prefs and prefs.eods.general.language and 
prefs.eods.general.language != 'None':
                langs.insert(0, prefs.eods.general.language)

        print "My prefered override", langs
        return langs

Zope3-users mailing list

Reply via email to