Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#6338 closed defect (fixed)

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:
Blocked By: Blocking:
Deployments affected: Action Needed: review
Verified: no


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

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 (1)

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

Download all attachments as: .zip

Change History (12)

comment:1 Changed 9 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)

comment:2 Changed 9 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...

comment:3 Changed 9 years ago by gregorio

  • Milestone Never Assigned deleted

Milestone Never Assigned deleted

comment:4 Changed 9 years ago by pierre

  • Action Needed 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:
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" %A %x %c")
print time.strftime("time %A %x %c")
locale.setlocale(locale.LC_ALL, "")
print" %A %x %c")
print time.strftime("time %A %x %c")


$ ./ 
TZ=America/Montreal Thursday 09/18/08 Thu Sep 18 00:15:24 2008
time Thursday 09/18/08 Thu Sep 18 00:15:24 2008 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?

comment:5 Changed 9 years ago by pierre

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

comment:6 Changed 9 years ago by marco

The following works for me:

#!/usr/bin/env python
import gtk
from datetime import datetime

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?

comment:7 Changed 9 years ago by pierre

Since build 653 when I developped the 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 (, 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...

comment:8 Changed 9 years ago by marco

Test activity source would be great.

comment:9 Changed 9 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 9 years ago by pierre

Test activity to demonstrate the problem

comment:10 Changed 9 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.

comment:11 Changed 8 years ago by pierre

  • Resolution set to fixed
  • Status changed from new to closed

This bug does not exist anymore in 8.2-762

Note: See TracTickets for help on using tickets.