[Zope3-Users] Problem with zope.i18n.locales, currency formatting, and encodings

Doyon, Jean-Francois jdoyon at NRCan.gc.ca
Fri Dec 22 13:41:54 EST 2006


For posterity and the list archive:

- currency formatting, as provided by zope.i18n.locales is broken, it does not add the proper currency symbol.  Instead, a strange circle/star character always appears where a currency symbol would be expected.

- This symbol can be found in the xml locale definition files in zope/i18n/locales/data ... In the <pattern> section for currencies.

- The xml files are correct.

- The problem is in the function at zope.i18n.format.parseNumberPattern, which does not handle the special symbol properly, and does not replace it with the symbol character as it should.

- Documentation on this can be found here: http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html (at the bottom especially).

- One work around for simple uses:

	- Copy the XML locale files you're interested in into your project/product/add-on.
	- Edit the pattern to replace the character with your actual currency symbol.
	- Instead of importing zope.i18n.locales.locales, create your own using LocaleProvider.

Here is how I did it in my Zope 2.10 product:

# Workaround for the bad locale data in zope.i18n.locales.data
# from zope.i18n.locales import locales
import os
from Globals import package_home
from zope.i18n.locales.provider import LocaleProvider
locales = LocaleProvider(os.path.join(package_home(globals()), 'locales', 'data'))

A bug has been filed here:

http://www.zope.org/Collectors/Zope3-dev/742

Cheers!

J.F.

-----Original Message-----
From: zope3-users-bounces at zope.org [mailto:zope3-users-bounces at zope.org] On Behalf Of Doyon, Jean-Francois
Sent: December 22, 2006 11:04 AM
To: zope3-users at zope.org
Subject: RE: [Zope3-Users] Problem with zope.i18n.locales, currency formatting,and encodings

Ah, I see the problem ... The pattern definitions in the XML locale data files are wrong and have that bizarre character hard coded in.

Is there documentation on these patterns?  What would I have to put in that pattern to get the symbol to show up?

I'm thinking I'll just setup my own XML locale definition files until this is fixed at the source ... But then if I do that I suppose I can hardcode the $ sign! :)

Thanks,
J.F.

-----Original Message-----
From: zope3-users-bounces at zope.org [mailto:zope3-users-bounces at zope.org] On Behalf Of Doyon, Jean-Francois
Sent: December 22, 2006 10:23 AM
To: zope3-users at zope.org
Subject: [Zope3-Users] Problem with zope.i18n.locales, currency formatting,and encodings

Hello,

I'm using the Zope 3 in Zope 2.10 ... (So Zope 3.3 I think?).

I'm trying to use the locales package to format currency (and other numbers) into locale sensitive strings.

I've figured out the API:

>>> from zope.i18n.locales import locales locale = 
>>> locales.getLocale('en', 'CA') formatter =
>>> locale.numbers.getFormatter('currency')
>>> formatter.format(29.95)
u'\xa429.95'
>>> locale.numbers.currencies['CAD'].symbol
u'$'

The problem is that bizarre encoding: \xa4 ... The dollar sign being standard 7-bit ascii, I'm not sure why it gets encoded this way ... And the symbol definition itself IS correct!

In my ZPT that symbol looks like something of a star with a circle in the middle ... Nothing anywhere near a dollar sign :)

I've tried some decode/encode options, but to no avail:

>>> cs.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa4' in position 0: ordinal not in range(128)
>>> cs.encode('latin1')
'\xa429.95'
>>> cs.encode('utf-8')
'\xc2\xa429.95'
>>> cs.encode('utf-8').decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)

What am I missing here?

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
_______________________________________________
Zope3-users mailing list
Zope3-users at zope.org
http://mail.zope.org/mailman/listinfo/zope3-users
_______________________________________________
Zope3-users mailing list
Zope3-users at zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


More information about the Zope3-users mailing list