Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (70 sloc) 2.63 KB

Localization (L10n)

Playdoh comes with all the libraries and tools you will need in production or development for localization.

Localization works by searching for localizable text in your source code and extracting (or merging them) into .po files in locale/.

Requirements

The one set of low level tools not provided is gettext.

# for ubuntu/debian
aptitude install gettext

# for mac
brew install gettext

Testing It Out, Right Now

Playdoh comes with a sample app called examples which is also localized into French. This is so that you can get the hang of how things work out of the box. To get started, simply follow these quick steps:

  1. ./bin/compile-mo.sh locale/
  2. Set DEV=True in settings_local.py
  3. ./manage.py runserver
  4. Point your browser to http://127.0.0.1:8000/fr

Installation

If you don't have any localization you need to do the following:

  1. ./manage.py extract -c
  2. ./manage.py merge -c

This will populate your locale/ directory.

Usage

Once your directories are set up you'll need to tend to your strings and their localization.

Strings

You can localize strings both in python code as well as Jinja templates.

In python:

from tower import ugettext as _, ugettext_lazy as _lazy

yodawg = _lazy('The Internet')

def myview(request):
    return render('template.html', {msg=_('Hello World'), msg2=yodawg})

_lazy is used when we are not in scope of a request. This lets us evaluate a string, such as yodawg, at the last possible second when we finally can draw upon the request's context. E.g. in a template:

{{ msg2 }}

Will be evaluated to whatever The Internet is in the requester's locale.

In Jinja we can use the following syntax for localized strings:

<h1>{{ _('Hello') }}</h1>

{% trans link='http://mozilla.org' %}
<p>Go to this <a href="{{ link }}">site</a>.</p>
{% endtrans %}

Updating our strings

If you make changes to your strings you can update them:

./manage.py extract
./manage.py merge

Note: you do not need -c for either command if you've created the required directories.

If you want to see these changes in your project:

./bin/compile-mo.sh locale/

Creating New Locales

In your projects settings.py add the new locale to KNOWN_LANGUAGES.

Then run ./manage.py merge -c which will create directories for any locales that are missing in /locale.