Ticket #6338 (closed defect: fixed)

Opened 6 years ago

Last modified 5 years ago

sugar.activity class should call locale.setlocale()

Reported by: pierre Owned by: sayamindu
Priority: normal Milestone:
Component: localization Version: Q2D16
Keywords: french date format localization locale Cc:
Action Needed: review Verified: no
Deployments affected: Blocked By:
Blocking:

Description

When changing the locale to French/France, the date format is not correct.

On the XO, I get:

$ date
lun fév  4 23:38:11 EST 2008

I would expect a more Frenchy format like the one I get on my Debian server (notice that the day number "4" is between the day name "lundi" and the month name "février"):

$ date
lundi 4 février 2008, 23:33:09 (UTC-0500)

Also, that small python program show that dates are displayed the American way (Month/Day/Year: 02/04/08) instead of the French one (Day/Month/Year: 04/02/08):

#!/usr/bin/env python
from datetime import datetime
print datetime.now().strftime("%x")

In an activity, this date is displayed "04.02.2008" when one would expect "04/02/2008" (or "04/02/08" like run from the command line).

When changing the locale to French/Canada, problems are similar but we should get the same formats as French/France. From the command line, results are as bad. From an activity, the date is displayed like "2008-02-05", which is definitely not usual.

Attachments

Test-1.xo (2.0 kB) - added by pierre 6 years ago.
Test activity to demonstrate the problem

Change History

Changed 6 years ago by AlbertCahalan

2008-02-05 is an ISO standard, intended to be unambiguous everywhere. It also sorts well, at least until 10000-01-01.

Since the existance of localized dates is not immediately obvious (not all software is fully localized), the traditional numeric formats are all bad. (US, French, and Japanese) Only one day per year would be unambiguous that way. (08/08/08, 09/09/09, 10/10/10, 11/11/11, 12/12/12)

Changed 6 years ago by pierre

Albert, what's the reason for such comment? Are you arguing that we should all use esperanto (some would say US English...) and ISO formats to reduce the amount of problems on that planet? I hadn't seen the OLPC project under that vision...

Changed 6 years ago by gregorio

  • milestone deleted

Milestone Never Assigned deleted

Changed 6 years ago by pierre

  • next_action set to review
  • version changed from Build 653 to Q2D16

After a lot of googling, I've found at least the reasons for such a behavior with Python programs. The full explanation is here: http://docs.python.org/lib/module-locale.html A program will not be locale aware as long you don't import the locale module and call setlocale(), even if your environment is set with a locale!

So as the base class sugar.activity does not set the locale correctly according to the locale selected by the user in the control panel, you have to explicitly call setlocale() in your activity.

A test program: change your locale to a non en_US in the control panel.

#!/usr/bin/env python
import locale
import os
from datetime import datetime
import time
print "LANG=%s" % os.environ["LANG"]
print "TZ=%s" % os.environ["TZ"]
print datetime.now().strftime("datetime.now %A %x %c")
print time.strftime("time %A %x %c")
locale.setlocale(locale.LC_ALL, "")
print datetime.now().strftime("datetime.now %A %x %c")
print time.strftime("time %A %x %c")

Displays:

$ ./test.py 
LANG=fr_CA.utf8
TZ=America/Montreal
datetime.now Thursday 09/18/08 Thu Sep 18 00:15:24 2008
time Thursday 09/18/08 Thu Sep 18 00:15:24 2008
datetime.now jeudi 2008-09-18 jeu 18 sep 2008 00:15:24 
time jeudi 2008-09-18 jeu 18 sep 2008 00:15:24 EDT

Why the sugar.activity class does not set the locale? It already sets the messages according to the locale; why do half of the job?

Changed 6 years ago by pierre

  • summary changed from French date format is not correct to sugar.activity class should call locale.setlocale()

Changed 6 years ago by marco

The following works for me:

#!/usr/bin/env python
import gtk
from datetime import datetime
print datetime.now().strftime("%x")

Note the "import gtk" at the top. Activities imports gtk...

Have you seen any actual problem in activities which seems to be related to this?

Changed 6 years ago by pierre

Since build 653 when I developped the Clock activity (http://wiki.laptop.org/go/Clock_activity), I've had problem displaying the name of the day or the date on my XO which is localized for fr_CA. In prevision of 8.2, I'm presently running a recent joyride, and I decided to solve that long lasting bug in this activity.

I dont have my XO with me at the present time, so I can't give you more details. I'll update this ticket tonight with more information. For the time being, I just checked into the Clock activity code and yes I import gtk.

Yesterday night when I updated the bug report, I created a test activity from the Activity Tutorial (http://wiki.laptop.org/go/Activity_tutorial), with a datetime.strftime("%x") instead of the Hello Word message, and it also exhibits the same problem, even if it imports gtk too. I'll upload the source tonight too, if you want it.

I've tested your code snippet from the python command line and it works, without a call to setlocale(). But from my tests, it seems not to work in a Sugar activity context...

Changed 6 years ago by marco

Test activity source would be great.

Changed 6 years ago by marco

Oh also, can you try to turn security off (remove /etc/olpc-security and reboot) and see if it makes the problem go away?

Changed 6 years ago by pierre

Test activity to demonstrate the problem

Changed 6 years ago by pierre

I've uploaded my test activity.

With /etc/olpc-security present, I've to explicitely call setlocale(). With /etc/olpc-security deleted, date localization works as expected.

Changed 6 years ago by pierre

  • status changed from new to closed
  • resolution set to fixed

This bug does not exist anymore in 8.2-762

Note: See TracTickets for help on using tickets.