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/

: Coding

Amonのクラス一覧

tokuhiromさん謹製のAmonのクラス一覧。

ちなみに2010年1月24日現在の最新のmasterの構成なので、 今後の開発によってまた変わってくると思います。

# root
Amon

    # Amon core
    Container
    Declare
    Trigger

    # Model
    Factory
        DBI
        DataModel

    # View
    V
        TemplateBase
        MT
            Context
        TT
        JSON

    # Utility
    Util

    # Web
    Web
        # Controller
        Declare
        C

        # Server abstract
        Request
        Response

        # disaptcher
        Disaptcher
            HTTPxDispatcher
            PathAttrRouter

    # Piugin
    Plugin
        FillInForm
        HTTPSession
        MobileAgent
        MobileCharset

    # Manual
    Manual
        Intro.ja
        Tutorial.ja
        Components.ja
        HandleStaticFile.ja
        Mobile.ja
        FAQ.ja
        TODO.ja

あわせて読みたい:

: Coding

Module::Installでtestsの順番をソートする

use inc: :Module::Install;
tests_recursive;
tests( join(q{ }, split(m{\s}, tests)) );

という感じで一応できる。

けどなんかバッドノウハウな感じがしなくもない><

: Coding Tumblrからインポート

Perlで'&{}'をoverloadする

こんな感じのことをできるとしばらく前にConfig::Mergeのコード読んで知った。

package MyClass;

use overload
    '&{}'       => sub { my $self = shift; return sub { $self->foo( @_ ) } },
    fallback    => 1;

sub new { bless {}, shift }
sub bar { 'bar' }

package main;

my $obj = MyClass->new;

print $obj->bar; # 'bar'
print $obj->();  # 'bar'

: Coding Tumblrからインポート