Browse Source

Talks localize dates

boyska 7 years ago
parent
commit
bc99069bee
5 changed files with 40 additions and 12 deletions
  1. 2 0
      content/pages/programma.en.rst
  2. 2 0
      content/pages/programma.rst
  3. 32 9
      plugins/talks.py
  4. 2 1
      requirements.txt
  5. 2 2
      talks/_templates/talk.html

+ 2 - 0
content/pages/programma.en.rst

@@ -18,7 +18,9 @@ Hackmeeting (still) hasn't a proper translation system, but you can
 find a bunch of people to ask to do translations when you need it.
 
 .. talkgrid::
+    :lang: en
 
 .. talklist::
+    :lang: en
 
 

+ 2 - 0
content/pages/programma.rst

@@ -13,6 +13,8 @@ Leggi l'`invito a presentare dei contenuti
 list <{filename}contatti.rst>`_
 
 .. talkgrid::
+    :lang: it
 
 .. talklist::
+    :lang: it
 

+ 32 - 9
plugins/talks.py

@@ -12,6 +12,9 @@ import datetime
 import shutil
 import time
 from copy import copy
+import locale
+from contextlib import contextmanager
+from babel.dates import format_date, format_datetime, format_time
 
 import markdown
 from docutils import nodes
@@ -42,6 +45,15 @@ def memoize(function):
     return wrapper
 
 
+@contextmanager
+def setlocale(name):
+    saved = locale.setlocale(locale.LC_ALL)
+    try:
+        yield locale.setlocale(locale.LC_ALL, name)
+    finally:
+        locale.setlocale(locale.LC_ALL, saved)
+
+
 @memoize
 def get_talk_names():
     return [name for name in os.listdir(TALKS_PATH)
@@ -140,18 +152,23 @@ jinja_env = jinja2.Environment(
 jinja_env.filters['markdown'] = lambda text: \
         jinja2.Markup(markdown.Markdown(extensions=['meta']).
                       convert(text))
+jinja_env.filters['dateformat'] = format_date
+jinja_env.filters['datetimeformat'] = format_datetime
+jinja_env.filters['timeformat'] = format_time
 
 
 class TalkListDirective(Directive):
-    required_arguments = 0
-    optional_arguments = 0
     final_argument_whitespace = True
     has_content = True
+    option_spec = {
+        'lang': directives.unchanged
+    }
 
     def run(self):
+        lang = self.options.get('lang', 'C')
         tmpl = jinja_env.get_template('talk.html')
         return [
-            nodes.raw('', tmpl.render(**get_talk_data(n)),
+            nodes.raw('', tmpl.render(lang=lang, **get_talk_data(n)),
                       format='html')
             for n in get_talk_names()
         ]
@@ -159,30 +176,34 @@ class TalkListDirective(Directive):
 
 class TalkDirective(Directive):
     required_arguments = 1
-    optional_arguments = 0
     final_argument_whitespace = True
     has_content = True
+    option_spec = {
+        'lang': directives.unchanged
+    }
 
     def run(self):
+        lang = self.options.get('lang', 'C')
         tmpl = jinja_env.get_template('talk.html')
         data = get_talk_data(self.arguments[0])
         if data is None:
             return []
         return [
-            nodes.raw('', tmpl.render(**data),
+            nodes.raw('', tmpl.render(lang=lang, **data),
                       format='html')
         ]
 
 
 class TalkGridDirective(Directive):
     '''A complete grid'''
-    required_arguments = 0
-
-    optional_arguments = 0
     final_argument_whitespace = True
     has_content = True
+    option_spec = {
+        'lang': directives.unchanged
+    }
 
     def run(self):
+        lang = self.options.get('lang', 'C')
         tmpl = jinja_env.get_template('grid.html')
         output = []
         days = unique_attr(all_talks(), 'day')
@@ -222,13 +243,15 @@ class TalkGridDirective(Directive):
                     times[position + i*GRID_STEP][roomnum] = copy(talk)
                     times[position + i*GRID_STEP][roomnum]['skip'] = True
 
+            #with setlocale(locale.normalize(lang)):
             render = tmpl.render(times=times,
                                  rooms=rooms,
                                  mintime=mintime, maxtime=maxtime,
                                  timestep=GRID_STEP,
+                                 lang=lang,
                                  )
             output.append(nodes.raw('', u'<h4>%s</h4>' %
-                                    day.strftime('%A %d').decode('utf8').title(),
+                                    format_date(day, format='full', locale=lang),
                                     format='html'))
             output.append(nodes.raw('', render, format='html'))
         return output

+ 2 - 1
requirements.txt

@@ -1,3 +1,4 @@
+Babel==2.4.0
 blinker==1.3
 docutils==0.12
 feedgenerator==1.7
@@ -8,8 +9,8 @@ pelican==3.5.0
 Pygments==2.0.2
 python-dateutil==2.4.1
 pytz==2014.10
+PyYAML==3.12
 six==1.9.0
 smartypants==1.8.6
 typogrify==2.0.7
 Unidecode==0.4.17
-PyYAML==3.12

+ 2 - 2
talks/_templates/talk.html

@@ -2,8 +2,8 @@
     <h3 class="talk-title">{{title}}</h3>
     <div class="talk-info">
     {% if time is defined and day is defined %}
-        {# Dai un nome meglio ai giorni #}
-        <p>Giorno {{day}} alle {{time.time()}}</p>
+        {# Vedi http://babel.pocoo.org/en/latest/dates.html #}
+        <p>{{day|dateformat(format='EEEE', locale=lang)}} - {{time.time()|timeformat(format='short', locale=lang)}}</p>
     {% else %}
         <p><i>L'orario non è ancora stato fissato</i></p>
     {% endif %}