[Zope3-Users] User preferences for languages- possible?

Giovannetti, Mark giovanne at nrcan.gc.ca
Fri Feb 23 16:07:40 EST 2007


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.

Regards,
Mark

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
    preferences.
    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.
    """

    implements(IModifiableUserPreferredLanguages)

    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
                try:
                    # 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]
                except:
                    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
        else:
            return False

    def _getPrefs(self):
        """
        Return the user's preference object.
        """
        try:
            prefs = UserPreferences()
            print "Prefs for", self.request.principal.id, prefs.eods.general.language
            return prefs
        except:
            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

========================================


More information about the Zope3-users mailing list