Commits

Joel Rivera committed 09ae6a4

Add i18n support.

  • Participants
  • Parent commits 3aebbc6

Comments (0)

Files changed (11)

 pyramid.debug_routematch = false
 pyramid.default_locale_name = en
 pyramid.includes =  pyramid_debugtoolbar
+available_languages = en es
 
 # By default, the toolbar only appears for clients from IP addresses
 # '127.0.0.1' and '::1'.

joelmx/__init__.py

+#import logging; log = logging.getLogger(__name__)
+
 from pyramid.config import Configurator
 
 
+
+def locale_businessman(req):
+    avail_langs = req.registry.settings['available_languages'].split()
+    if 'lang' in req.params \
+           and req.params['lang'] in avail_langs:
+        lang = req.params['lang']
+    else:
+        deflang = req.registry.settings['default_locale_name']
+        lang = req.accept_language.best_match(avail_langs, deflang)
+    req.response.vary = ('Content-Language', )
+    req.response.content_language = (lang, )
+    return lang
+
+
 def main(global_config, **settings):
     """ This function returns a Pyramid WSGI application.
     """
-    config = Configurator(settings=settings)
+    config = Configurator(settings=settings,
+                          locale_negotiator=locale_businessman)
+    config.add_translation_dirs('joelmx:locale/')
+    config.add_subscriber('joelmx.subscribers.add_renderer_globals',
+                          'pyramid.events.BeforeRender')
+    config.add_subscriber('joelmx.subscribers.add_localizer',
+                          'pyramid.events.NewRequest')
     config.add_static_view('/static', 'static', cache_max_age=3600)
     config.add_route('home', '/')
     config.scan()

joelmx/locale/es/LC_MESSAGES/joelmx.mo

Binary file added.

joelmx/locale/es/LC_MESSAGES/joelmx.po

+# Spanish translations for joelmx.
+# Copyright (C) 2013 ORGANIZATION
+# This file is distributed under the same license as the joelmx project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, 2013.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: joelmx 0.0\n"
+"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
+"POT-Creation-Date: 2013-02-10 03:25-0600\n"
+"PO-Revision-Date: 2013-02-10 03:31-0600\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: es <LL@li.org>\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Generated-By: Babel 1.0dev\n"
+
+#: joelmx/templates/home.mako:6
+msgid "Hi!, my name is Joel Rivera."
+msgstr "¡Hola!, me llamo Joel Rivera."
+
+#: joelmx/templates/home.mako:7
+msgid ""
+"I'm a software developer from Monterrey Mexico with a particular taste to"
+" software libre, python and coffee."
+msgstr "Soy un desarrollador de software de Monterrey México con un gusto particular por el software libre, python y el café."
+
+#: joelmx/templates/home.mako:8
+msgid "Here is my"
+msgstr "Aquí esta mí"
+
+#: joelmx/templates/home.mako:8
+msgid "and a bunch of other links"
+msgstr "y otro puñado de ligas"
+

joelmx/locale/joelmx.pot

+# Translations template for joelmx.
+# Copyright (C) 2013 ORGANIZATION
+# This file is distributed under the same license as the joelmx project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, 2013.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: joelmx 0.0\n"
+"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
+"POT-Creation-Date: 2013-02-10 03:25-0600\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Generated-By: Babel 1.0dev\n"
+
+#: joelmx/templates/home.mako:6
+msgid "Hi!, my name is Joel Rivera."
+msgstr ""
+
+#: joelmx/templates/home.mako:7
+msgid ""
+"I'm a software developer from Monterrey Mexico with a particular taste to "
+"software libre, python and coffee."
+msgstr ""
+
+#: joelmx/templates/home.mako:8
+msgid "Here is my"
+msgstr ""
+
+#: joelmx/templates/home.mako:8
+msgid "and a bunch of other links"
+msgstr ""
+

joelmx/static/css/main.css

 a {
     color: #73FF00;
     text-decoration: none;
+}
+
+
+body > div {
+    margin-top: 20px;
+}
+
+
+#bio {
+    text-align: left; 
+}
+#bio > img {
+    float: left; 
+    margin-right: 20px;
+}
+
+#links {
+    float: left;
+    width: 100%;
+    margin-bottom: 40px;
+}
+
+#links > p {
+    text-align:left;
+}
+#links > p > a {
+    font-weight: bold
+}
+#links > div {
+    margin: 0 auto; 
+    width: 100%;
+}
+#content {
+    margin: 0 auto; 
+    text-align: center; 
+    height: 650px; 
+    max-width: 650px;
+}
+
+#languages {
+    width: 100%;
+    float: left;
+    margin-bottom: 20px;
+    
+}
+#languages ul {
+    list-style: none;
+    font-size: 12px;
+}
+#languages li {
+    float: right;
+    margin-left: 10px;
+}
+
+#twitter {
+    width: 100%;
 }

joelmx/subscribers.py

+from pyramid.i18n import get_localizer, TranslationStringFactory
+
+
+
+def add_renderer_globals(event):
+    request = event['request']
+    event['_'] = request.translate
+    event['localizer'] = request.localizer
+
+tsf = TranslationStringFactory('joelmx')
+
+def add_localizer(event):
+    request = event.request
+    localizer = get_localizer(request)
+    def auto_translate(*args, **kwargs):
+        return localizer.translate(tsf(*args, **kwargs))
+    request.localizer = localizer
+    request.translate = auto_translate

joelmx/templates/base.mako

         <!--[if lt IE 7]>
             <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
         <![endif]-->
-	<div class="container" style="margin-top: 40px;">
-	${next.body()}
-	</div>
+	<div>${next.body()}</div>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
         <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.0.min.js"><\/script>')</script>
         <script src="${request.static_url('joelmx:static/js/plugins.js')}"></script>

joelmx/templates/home.mako

 <%inherit file="base.mako" />
 
-<div style="margin: 0 auto; text-align: center; height: 650px; max-width: 650px;">
-<div class="bio" style="text-align:left">
-  <img style="float: left; margin-right: 20px;" src="http://www.gravatar.com/avatar/246eb04589798870601ddf54de87153d.png?s=125" />
-  <h3>Hi!, my name is Joel Rivera.</h3>
-  <h4>
-    I'm a software developer from Monterrey Mexico with a particular 
-    taste to software libre, python and coffee.
-  </h4>
-  <p>Here is my <a href="http://blog.joel.mx/">blog</a> and a bunch of other links:</p>  
+<div id="content">
+
+  <div id="languages">
+    <ul>
+      <li><a href="/?lang=es"> Español</a></li>
+      <li><a href="/?lang=en"> English</a></li>
+    </ul>
+  </div>
+
+  <div id="bio">
+    <img src="http://www.gravatar.com/avatar/82ab5359b67025a1a2debde9e7de7b4a.png?s=125" />
+    <h3>${_("Hi!, my name is Joel Rivera.")}</h3>
+    <h4>${_("I'm a software developer from Monterrey Mexico with a particular taste to software libre, python and coffee.")} </h4>
+  </div>
+  
+  
+  <div id="links">
+    <p>
+      ${_("Here is my")}
+      <a href="http://blog.joel.mx/">blog</a>
+      ${_("and a bunch of other links")}:
+    </p>  
+    
+    <div>
+      <a href="http://www.linkedin.com/in/joelriv" >
+	<img alt="LinkedIn" src="${request.static_url('joelmx:static/img/linkedin.png')}" >
+      </a>
+      <a href="http://stackoverflow.com/users/298371/cyraxjoe">
+	<img alt="StackOverflow"
+	     src="${request.static_url('joelmx:static/img/stackoverflow.png')}" >
+      </a>
+      <a href="https://bitbucket.org/cyraxjoe/">
+	<img alt="Bitbucket"
+	     src="${request.static_url('joelmx:static/img/bitbucket.png')}" >
+      </a>
+      <a href="https://github.com/cyraxjoe">
+	<img alt="Github"
+	     src="${request.static_url('joelmx:static/img/github.png')}" >
+      </a>
+      <a href="http://geekli.st/cyraxjoe">
+	<img alt="GeekList"
+	     src="${request.static_url('joelmx:static/img/geeklist.png')}" >
+      </a>
+    </div>
+  </div>
+  
+  <div id="twitter">
+    <%include file="_tw_widget.mako" />
+  </div>
+
+
 </div>
 
-
-  <div>
-    <a href="http://www.linkedin.com/in/joelriv" alt="LinkedIn">
-      <img src="${request.static_url('joelmx:static/img/linkedin.png')}" >
-    </a>
-    <a href="http://stackoverflow.com/users/298371/cyraxjoe">
-      <img src="${request.static_url('joelmx:static/img/stackoverflow.png')}" >
-    </a>
-    <a href="https://bitbucket.org/cyraxjoe/">
-      <img src="${request.static_url('joelmx:static/img/bitbucket.png')}" >
-    </a>
-    <a href="https://github.com/cyraxjoe">
-      <img src="${request.static_url('joelmx:static/img/github.png')}" >
-    </a>
-    <a href="http://geekli.st/cyraxjoe">
-      <img src="${request.static_url('joelmx:static/img/geeklist.png')}" >
-    </a>
-  </div>
-  <div style="margin-top: 36px;">
-    <%include file="_tw_widget.mako" />
-  </div>
-</div>
-
 pyramid.debug_notfound = false
 pyramid.debug_routematch = false
 pyramid.default_locale_name = en
-
+available_languages = en es
 ###
 # wsgi server configuration
 ###
 README = open(os.path.join(here, 'README')).read()
 
 requires = [
+    'Babel',
     'pyramid',
     'pyramid_debugtoolbar',
     'waitress',
       [paste.app_factory]
       main = joelmx:main
       """,
+      message_extractors = {
+          'joelmx': [('**.py', 'python', None),
+                     ('templates/**.mako', 'mako', None),
+                     ('static/**', 'ignore', None)]},
       )