カラクリスタ

『輝かしい青春』なんて失かったヒトのブログ

How to use tipfy.ext.i18n

0. about

This document is how to i18n in tipfy.

1. fist setup.

setup your tipfy application

2. edit config.py

config                      = {}

config['tipfy']             = {
    # middleware
    'middleware': [
        'tipfy.ext.debugger.DebuggerMiddleware',
        'tipfy.ext.i18n.I18nMiddleware',        # <- Added.
    ],

    # application
    'apps_installed': [
        'apps.hello_world',
    ],
}

# added
config['tipfy.ext.i18n']    = {
    'locale':                   'ja_JP',
    'timezone':                 'Asia/Tokyo',
    'cookie_name':              'trapezo.locale',
    'locale_request_lookup':    [
        ( 'args',   'lang'          ),
        ('cookies', 'trapezo.locale'),
    ],
}

3. edit po

locale/en_US.po

msgid "welcome"
msgstr "fooooooooooooo!"

4. compile po to mo and move

$ cd locale
$ msgfmt en_US.po
$ mkdir -p en_US/LC_MESSAGES
$ mv messages.mo en_US/LC_MESSAGES

5. gettext !

in your application

# -*- coding: utf-8 -*-
"""
    handlers
    ~~~~~~~~

    Hello, World!: the simplest tipfy app.

    :copyright: 2009 by tipfy.org.
    :license: BSD, see LICENSE for more details.
"""
from tipfy import RequestHandler, Response
from tipfy.ext.jinja2 import render_response
from tipfy.ext.i18n import _                # <- added

class HelloWorldHandler(RequestHandler):
    def get(self):
        """Simply returns a Response object with an enigmatic salutation."""
        return Response(_('Hello, World!')) # <- use `_` function

class PrettyHelloWorldHandler(RequestHandler):
    def get(self):
        """Simply returns a rendered template with an enigmatic salutation."""
        return render_response('hello_world.html', message=_('Hello, World!')) # <- use `_` function

6. finish

access to http://localhost:8080/