Re: Patch to add support for locales (for localized dates)

Martijn van de Streek <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
On Sun, 10 Sep 2006, Martijn van de Streek wrote:

> It's a new patch against clean 1.3.2 (so it probably won't apply if
> you've already applied the previous patch)

But it's not clean.. as it contains some artifacts of my tries to get
the Atom flavour to validate. *sigh*

Here's a new, clean patch, this time against SVN trunk..

Martijn
-- 
Sorry isn't an excuse when you do something stupid on purpose.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Pyblosxom-devel mailing list
Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
pyblosxom-locale-trunk.patch (text/plain, 5.7 KB)
Index: Pyblosxom/pyblosxom.py
===================================================================
--- Pyblosxom/pyblosxom.py	(revision 929)
+++ Pyblosxom/pyblosxom.py	(working copy)
@@ -22,6 +22,7 @@
 import re
 import sys
 import os.path
+import locale 
 import cgi
 try: 
     from cStringIO import StringIO
@@ -82,6 +83,12 @@
         pyhttp = self._request.getHttp()
         config = self._request.getConfiguration()
 
+        # Initialize the locale, if wanted (will silently fail if locale is not
+        # available)
+        if config.get('locale'):
+            try: locale.setlocale(locale.LC_ALL, config['locale'])
+            except locale.Error: pass
+
         # initialize the tools module
         tools.initialize(config)
 
@@ -887,11 +894,18 @@
     mtime_gmtuple = time.gmtime(mtime)
 
     data["latest_date"] = time.strftime('%a, %d %b %Y', mtime_tuple)
+
+    # Make sure we get proper 'English' dates when using standards
+    loc = locale.getlocale(locale.LC_ALL)
+    locale.setlocale(locale.LC_ALL, 'C')
+
     data["latest_w3cdate"] = time.strftime('%Y-%m-%dT%H:%M:%SZ', 
                                            mtime_gmtuple)
     data['latest_rfc822date'] = time.strftime('%a, %d %b %Y %H:%M GMT', 
                                               mtime_gmtuple)
 
+    locale.setlocale(locale.LC_ALL, loc)
+
     # we pass the request with the entry_list through the prepare callback
     # giving everyone a chance to transform the data.  the request is
     # modified in place.
Index: Pyblosxom/tools.py
===================================================================
--- Pyblosxom/tools.py	(revision 929)
+++ Pyblosxom/tools.py	(working copy)
@@ -28,6 +28,7 @@
 import time
 import os.path
 import sys
+import locale
 import urllib
 
 try:
@@ -39,30 +40,10 @@
 # Pyblosxom imports
 import plugin_utils
 
-month2num = { 'nil' : '00',
-              'Jan' : '01',
-              'Feb' : '02',
-              'Mar' : '03',
-              'Apr' : '04',
-              'May' : '05',
-              'Jun' : '06',
-              'Jul' : '07',
-              'Aug' : '08',
-              'Sep' : '09',
-              'Oct' : '10',
-              'Nov' : '11',
-              'Dec' : '12'}
+month2num = None
+num2month = None
+MONTHS    = None
 
-# This is not python 2.1 compatible (Nifty though)
-# num2month = dict(zip(month2num.itervalues(), month2num))
-num2month = {}
-for month_abbr, month_num in month2num.items():
-    num2month[month_num] = month_abbr
-    num2month[int(month_num)] = month_abbr
-
-# all the valid month possibilities
-MONTHS = num2month.keys() + month2num.keys()
-
 # regular expression for detection and substituion of variables.
 VAR_REGEXP = re.compile(ur'(?<!\\)\$((?:\w|\-|::\w)+(?:\(.*?(?<!\\)\))?)')
 
@@ -79,6 +60,34 @@
     global _config
     _config = config
 
+    # Month names tend to differ with locale
+    global month2num
+    month2num = { 'nil' : '00',
+                  locale.nl_langinfo(locale.ABMON_1) : '01',
+                  locale.nl_langinfo(locale.ABMON_2) : '02',
+                  locale.nl_langinfo(locale.ABMON_3) : '03',
+                  locale.nl_langinfo(locale.ABMON_4) : '04',
+                  locale.nl_langinfo(locale.ABMON_5) : '05',
+                  locale.nl_langinfo(locale.ABMON_6) : '06',
+                  locale.nl_langinfo(locale.ABMON_7) : '07',
+                  locale.nl_langinfo(locale.ABMON_8) : '08',
+                  locale.nl_langinfo(locale.ABMON_9) : '09',
+                  locale.nl_langinfo(locale.ABMON_10) : '10',
+                  locale.nl_langinfo(locale.ABMON_11) : '11',
+                  locale.nl_langinfo(locale.ABMON_12) : '12'}
+    
+    # This is not python 2.1 compatible (Nifty though)
+    # num2month = dict(zip(month2num.itervalues(), month2num))
+    global num2month
+    num2month = {}
+    for month_abbr, month_num in month2num.items():
+        num2month[month_num] = month_abbr
+        num2month[int(month_num)] = month_abbr
+    
+    # all the valid month possibilities
+    global MONTHS
+    MONTHS = num2month.keys() + month2num.keys()
+
 def cleanup():
     """
     Cleanup the tools module.
Index: Pyblosxom/entries/base.py
===================================================================
--- Pyblosxom/entries/base.py	(revision 929)
+++ Pyblosxom/entries/base.py	(working copy)
@@ -21,6 +21,7 @@
 __revision__ = "$Revision$"
 
 import time
+import locale
 from Pyblosxom import tools
 
 BIGNUM = 2000000000
@@ -203,12 +204,18 @@
         self['dw'] = time.strftime('%A', timetuple)
         self['yr'] = time.strftime('%Y', timetuple)
         self['fulltime'] = time.strftime('%Y%m%d%H%M%S', timetuple)
+        
+        self['date'] = time.strftime('%a, %d %b %Y', timetuple)
 
+        loc = locale.getlocale(locale.LC_ALL)
+        locale.setlocale(locale.LC_ALL, 'C')
+
         # YYYY-MM-DDThh:mm:ssZ
         self['w3cdate'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', gmtimetuple)
         self['rfc822date'] = time.strftime('%a, %d %b %Y %H:%M GMT', \
                                            gmtimetuple)
-        self['date'] = time.strftime('%a, %d %b %Y', timetuple)
+        
+        locale.setlocale(locale.LC_ALL, loc)
 
     def __getitem__(self, key, default=None):
         """
Index: web/config.py
===================================================================
--- web/config.py	(revision 929)
+++ web/config.py	(working copy)
@@ -8,6 +8,11 @@
 # directory.
 #py["codebase"] = "/path/to/pyblosxom/installation"
 
+# What's the locale for the blog?
+# This affects things like the format of dates, etc.
+# Not required, but if you enter one, be sure it's valid for your system. 
+#py["locale"] = "nl_NL.UTF-8"
+
 # What's this blog's title?
 py['blog_title'] = "Another pyblosxom blog"
signature.asc (application/pgp-signature, 191 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFA72nDafvoz+l4DERAn/TAJwI8emLiV/Apk6h/fBlqJNb8m+wSACfX1j1
6stsBfMeNjJXoVkiw6q2FOA=
=PuHw
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.