Question

I would like to display the date and time (or either date or time, separately) in multiple languages. Specifically English and Spanish, but perhaps in other languages in the future.

I've considered pythons time.strftime function in combination with locale.setlocal. I ruled this out after reading in the docs (http://docs.python.org/library/locale.html) that libraries should not call locale.setlocal, so I shouldn't just "flip flop" (change, and then restore) the locale settings in my library.

I've also considered the python library Babel, but I read in their docs (http://babel.edgewall.org/wiki/Documentation/0.9/dates.html) that date parsing "isn't properly implemented" which concerns me. I do need date parsing abilities. However, Babel can display the time using different locales.

A third [hybrid] option would be to use pythons datetime to parse the date, and then Babel to display the date in multiple languages/locales. This is a little messy because strptime uses it's own format string syntax, while Babel uses LDML syntax for its format strings.

There may be other options. The question again: What is the best way to display a date or time in multiple languages?

Was it helpful?

Solution

I would maintain the translations as standard PO file using the gettext mechanism. For each language you keep the translations in a dedicated file. You use the month and day names as msgid and keep the specific translations as msgstr.

http://docs.python.org/library/gettext.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top