Changes between Version 31 and Version 32 of SandBox


Ignore:
Timestamp:
02/29/2012 04:33:55 PM (6 years ago)
Author:
apoirier
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SandBox

    v31 v32  
    3939in two languages (French and English) in order to demonstrate how to switch the language at runtime. 
    4040 
    41 First, install Nagare with the ``i18n`` option:: 
     41First, install Nagare with the ``i18n`` option: 
     42 
     43.. code-block:: sh 
    4244 
    4345    $ easy_install nagare[i18n] 
    4446 
    4547 
    46 Then, create a sample application:: 
     48Then, create a sample application: 
     49 
     50.. code-block:: sh 
    4751 
    4852    $ nagare create-app i18n_example 
     
    5155     
    5256     
    53 Edit the app.py file, import the ``nagare.i18n._`` function and mark the messages to be translated with it:: 
     57Edit the app.py file, import the ``nagare.i18n._`` function and mark the messages to be translated with it: 
     58 
     59.. code-block:: python 
    5460 
    5561    # -*- coding: utf-8 -*- 
     
    8894 
    8995 
    90 Now, extract the messages and create the localized versions of the applications messages:: 
     96Now, extract the messages and create the localized versions of the applications messages: 
     97 
     98.. code-block:: sh 
    9199 
    92100    $ mkdir data/locale 
     
    95103    $ python setup.py init_catalog -l fr_FR 
    96104 
    97 Edit the ``data/locale/<lang>/LC_MESSAGES/messages.po`` files and fill in the missing translations, then type:: 
    98      
     105Edit the ``data/locale/<lang>/LC_MESSAGES/messages.po`` files and fill in the missing translations, then type: 
     106     
     107.. code-block:: sh 
     108 
    99109    $ python setup.py compile_catalog 
    100110 
     
    111121browser between two requests), the proper place to change the locale of the application is in the ``start_request`` 
    112122method of the ``wsgi.WSGIApp`` class. So, we must first introduce an application class that inherit from Nagare's 
    113 ``nagare.wsgi.WSGIApp`` class:: 
     123``nagare.wsgi.WSGIApp`` class: 
     124 
     125.. code-block:: python 
    114126 
    115127    from nagare import wsgi, i18n 
     
    135147to the default locale. 
    136148 
    137 Then, we use an instance of ``I18nExampleApp`` as our app:: 
     149Then, we use an instance of ``I18nExampleApp`` as our app: 
     150 
     151.. code-block:: python 
    138152 
    139153    from nagare import component 
     
    142156 
    143157 
    144 Serve the application:: 
     158Serve the application: 
     159 
     160.. code-block:: sh 
    145161 
    146162    $ nagare-admin serve i18_example --reload 
     
    156172user in his profile. As you will see, it doesn't really matter where the locale setting comes from. 
    157173 
    158 Let's start. First, create a view with the language change links:: 
     174Let's start. First, create a view with the language change links: 
     175 
     176.. code-block:: python 
    159177 
    160178    @presentation.render_for(I18nExample, model='locale_change') 
     
    172190        return h.root 
    173191 
    174 Also, don't forget to render the ``locale_change`` view in the default view:: 
     192Also, don't forget to render the ``locale_change`` view in the default view: 
     193 
     194.. code-block:: python 
    175195 
    176196    with h.div(id='content'): 
     
    179199 
    180200Nothing hard there: we added a view to the ``I18nExample`` component (the root component of our application) that shows two HTML ``<a>`` 
    181 elements which set the locale to either English or French. The ``set_locale`` method has not been defined yet, so let us define it:: 
     201elements which set the locale to either English or French. The ``set_locale`` method has not been defined yet, so let us define it: 
     202 
     203.. code-block:: python 
    182204 
    183205    class I18nExample(object): 
     
    203225``locale_override`` attribute in order to re-install this locale in subsequent requests. 
    204226 
    205 The last step is to re-install the overridden locale in each request, so let's go back to the ``start_request`` method:: 
     227The last step is to re-install the overridden locale in each request, so let's go back to the ``start_request`` method: 
     228 
     229.. code-block:: python 
    206230 
    207231    def start_request(self, root, request, response):