A Solution to: [Zope] Zope, setlocale(), requests and traversal

Doyon, Jean-Francois jdoyon at NRCan.gc.ca
Thu Dec 14 11:25:55 EST 2006


Hello,

OK, I found out the issues, and came up with a solution, if any one cares :)

Requirements:

- A get_request patch
- A REQUEST object that will have the necessary information to locaize from (language code, etc ...)

How I do it:

- Copy Python lib's locale.py to your product
- import Globals
- Force "locale emulation", I do it by raising an ImportError when importing _locale
- Hardcode the dicts that contain the usual localconv() output for the locales you're interested in
- Edit the localeconv() emulation to get the request, and from there get the localization information somehow (I base it on HTTP_ACCEPT_LANGUAGE for example), and then return the dict information.
- import locale in __init__.py

Works great for me, and was pretty straightforward.

Notes:

- You might be able to do this through monkey patching ... I copy the module because I'm using 2.5's locale module with 2.4 ... To get currency() support.
- You might want to put Module security declarations in there somewhere.
- Nice benefit that you can easily change the number/currency parameters to your liking, if needed.

Some code (Sorry can't just share/open source it all, due to my employer's policies):

<...>
# Zope imports
import Globals
from Products.CMFCore.utils import getToolByName
<...>
try:

    # from _locale import *
    raise ImportError

except ImportError:

    # Locale emulation
<...>
    def localeconv():
        """ localeconv() -> dict.
            Returns numeric and monetary locale-specific parameters.
        """
        request = Globals.get_request()
        portal_localizer = getToolByName(request.get('PUBLISHED'), 'portal_localizer')
        locale_suffix = portal_localizer.getProperty('locale_suffix')
        return LOCALECONV[request.get('HTTP_ACCEPT_LANGUAGE', 'en') + locale_suffix]
<...>
LOCALECONV = {'en_CA':
              {'mon_decimal_point': '.',
               'int_frac_digits': 2,
               'p_sep_by_space': 0,
               'frac_digits': 2,
               'thousands_sep': ',',
               'n_sign_posn': 1,
               'decimal_point': '.',
               'int_curr_symbol': 'CAD ',
               'n_cs_precedes': 1,
               'p_sign_posn': 1,
               'mon_thousands_sep': ',',
               'negative_sign': '-',
               'currency_symbol': '$',
               'n_sep_by_space': 0,
               'mon_grouping': [3, 3, 0],
               'p_cs_precedes': 1,
               'positive_sign': '',
               'grouping': [3, 3, 0]},
              'fr_CA':
              {'mon_decimal_point': ',',
               'int_frac_digits': 2,
               'p_sep_by_space': 1,
               'frac_digits': 2,
               'thousands_sep': ' ',
               'n_sign_posn': 0,
               'decimal_point': ',',
               'int_curr_symbol': 'CAD ',
               'n_cs_precedes': 0,
               'p_sign_posn': 1,
               'mon_thousands_sep': ' ',
               'negative_sign': '-',
               'currency_symbol': '$',
               'n_sep_by_space': 1,
               'mon_grouping': [3, 3, 0],
               'p_cs_precedes': 0,
               'positive_sign': '',
               'grouping': []}
             }
</...>

Hope this comes in handy to someone!

J.F.

-----Original Message-----
From: zope-bounces at zope.org [mailto:zope-bounces at zope.org] On Behalf Of Doyon, Jean-Francois
Sent: December 14, 2006 9:23 AM
To: zope at zope.org
Subject: [Zope] Zope, setlocale(), requests and traversal

Hello,

I know there is a locale setting in etc/zope.conf, and I make use of it.

The problem now, is that I have a multilingual site that I would like to have support multiple locales, per request:  One request could be for the french site and content, using the fr_CA locale in my case, but another could be for english, and en_CA ... Localization is done through a traversal hook.

So right now I call setlocale() on a per request basis, and it works fine in development.

So I'm wondering:

- I see the docs mention setlocale might not be threadsafe, since Zope is threaded, anyone know the implications?
- If I call setlocale() during traversal, could that setting affect other threads/requests?
- If the above 2 are indeed problems, is there some nice way to do set per-request locales, short of completely implementing a custom way of doing it?

Thanks!

Jean-François Doyon
Internet Service Development and Systems Support / Spécialiste de dèveloppements internet et soutien technique Canada Centre for Remote Sensing/Centre Canadien de télédétection Natural Resources Canada/Ressources Naturelles Canada http://atlas.gc.ca
Tel./Tél.: (613) 992-4902
Fax: (613) 947-2410
_______________________________________________
Zope maillist  -  Zope at zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


More information about the Zope mailing list