2008-08-24 06:25:40 +08:00
|
|
|
.. _topics-i18n:
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
====================
|
|
|
|
Internationalization
|
|
|
|
====================
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Django has full support for internationalization of text in code and templates.
|
2005-11-05 12:40:07 +08:00
|
|
|
Here's how it works.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Overview
|
|
|
|
========
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
The goal of internationalization is to allow a single Web application to offer
|
|
|
|
its content and functionality in multiple languages.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
You, the Django developer, can accomplish this goal by adding a minimal amount
|
|
|
|
of hooks to your Python code and templates. These hooks are called
|
|
|
|
**translation strings**. They tell Django: "This text should be translated into
|
|
|
|
the end user's language, if a translation for this text is available in that
|
|
|
|
language."
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Django takes care of using these hooks to translate Web apps, on the fly,
|
|
|
|
according to users' language preferences.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Essentially, Django does two things:
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
* It lets developers and template authors specify which parts of their apps
|
|
|
|
should be translatable.
|
|
|
|
* It uses these hooks to translate Web apps for particular users according
|
|
|
|
to their language preferences.
|
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
If you don't need internationalization in your app
|
|
|
|
==================================================
|
2006-07-01 11:09:14 +08:00
|
|
|
|
|
|
|
Django's internationalization hooks are on by default, and that means there's a
|
|
|
|
bit of i18n-related overhead in certain places of the framework. If you don't
|
|
|
|
use internationalization, you should take the two seconds to set
|
2008-08-24 06:25:40 +08:00
|
|
|
:setting:`USE_I18N = False <USE_I18N>` in your settings file. If
|
|
|
|
:setting:`USE_I18N` is set to ``False``, then Django will make some
|
|
|
|
optimizations so as not to load the internationalization machinery.
|
2006-07-01 11:09:14 +08:00
|
|
|
|
2006-08-18 11:43:55 +08:00
|
|
|
You'll probably also want to remove ``'django.core.context_processors.i18n'``
|
|
|
|
from your ``TEMPLATE_CONTEXT_PROCESSORS`` setting.
|
2006-07-01 11:09:14 +08:00
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
If you do need internationalization: three steps
|
|
|
|
================================================
|
|
|
|
|
|
|
|
1. Embed translation strings in your Python code and templates.
|
|
|
|
2. Get translations for those strings, in whichever languages you want to
|
|
|
|
support.
|
|
|
|
3. Activate the locale middleware in your Django settings.
|
|
|
|
|
|
|
|
.. admonition:: Behind the scenes
|
|
|
|
|
|
|
|
Django's translation machinery uses the standard ``gettext`` module that
|
|
|
|
comes with Python.
|
|
|
|
|
|
|
|
1. How to specify translation strings
|
|
|
|
=====================================
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Translation strings specify "This text should be translated." These strings can
|
|
|
|
appear in your Python code and templates. It's your responsibility to mark
|
|
|
|
translatable strings; the system can only translate strings it knows about.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
In Python code
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Standard translation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Specify a translation string by using the function ``ugettext()``. It's
|
|
|
|
convention to import this as a shorter alias, ``_``, to save typing.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Python's standard library ``gettext`` module installs ``_()`` into the
|
|
|
|
global namespace, as an alias for ``gettext()``. In Django, we have chosen
|
|
|
|
not to follow this practice, for a couple of reasons:
|
|
|
|
|
|
|
|
1. For international character set (Unicode) support, ``ugettext()`` is
|
|
|
|
more useful than ``gettext()``. Sometimes, you should be using
|
|
|
|
``ugettext_lazy()`` as the default translation method for a particular
|
|
|
|
file. Without ``_()`` in the global namespace, the developer has to
|
|
|
|
think about which is the most appropriate translation function.
|
|
|
|
|
|
|
|
2. The underscore character (``_``) is used to represent "the previous
|
|
|
|
result" in Python's interactive shell and doctest tests. Installing a
|
|
|
|
global ``_()`` function causes interference. Explicitly importing
|
|
|
|
``ugettext()`` as ``_()`` avoids this problem.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. highlightlang:: python
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
In this example, the text ``"Welcome to my site."`` is marked as a translation
|
|
|
|
string::
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
def my_view(request):
|
|
|
|
output = _("Welcome to my site.")
|
|
|
|
return HttpResponse(output)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Obviously, you could code this without using the alias. This example is
|
|
|
|
identical to the previous one::
|
|
|
|
|
|
|
|
from django.utils.translation import ugettext
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
def my_view(request):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
output = ugettext("Welcome to my site.")
|
2005-11-05 12:22:25 +08:00
|
|
|
return HttpResponse(output)
|
|
|
|
|
|
|
|
Translation works on computed values. This example is identical to the previous
|
|
|
|
two::
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
words = ['Welcome', 'to', 'my', 'site.']
|
|
|
|
output = _(' '.join(words))
|
|
|
|
return HttpResponse(output)
|
|
|
|
|
|
|
|
Translation works on variables. Again, here's an identical example::
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
sentence = 'Welcome to my site.'
|
|
|
|
output = _(sentence)
|
|
|
|
return HttpResponse(output)
|
|
|
|
|
2005-11-05 12:40:07 +08:00
|
|
|
(The caveat with using variables or computed values, as in the previous two
|
|
|
|
examples, is that Django's translation-string-detecting utility,
|
2008-07-06 14:39:44 +08:00
|
|
|
``django-admin.py makemessages``, won't be able to find these strings. More on
|
|
|
|
``makemessages`` later.)
|
2005-11-05 12:40:07 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
The strings you pass to ``_()`` or ``ugettext()`` can take placeholders,
|
2005-11-05 12:22:25 +08:00
|
|
|
specified with Python's standard named-string interpolation syntax. Example::
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
def my_view(request, m, d):
|
2008-10-05 14:17:29 +08:00
|
|
|
output = _('Today is %(month)s, %(day)s.') % {'month': m, 'day': d}
|
2005-11-05 12:22:25 +08:00
|
|
|
return HttpResponse(output)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
This technique lets language-specific translations reorder the placeholder
|
2008-09-04 04:52:20 +08:00
|
|
|
text. For example, an English translation may be ``"Today is November, 26."``,
|
|
|
|
while a Spanish translation may be ``"Hoy es 26 de Noviembre."`` -- with the
|
|
|
|
placeholders (the month and the day) with their positions swapped.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
For this reason, you should use named-string interpolation (e.g., ``%(day)s``)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
instead of positional interpolation (e.g., ``%s`` or ``%d``) whenever you
|
|
|
|
have more than a single parameter. If you used positional interpolation,
|
|
|
|
translations wouldn't be able to reorder placeholder text.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Marking strings as no-op
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Use the function ``django.utils.translation.ugettext_noop()`` to mark a string
|
2005-11-05 12:40:07 +08:00
|
|
|
as a translation string without translating it. The string is later translated
|
2005-11-05 12:22:25 +08:00
|
|
|
from a variable.
|
|
|
|
|
|
|
|
Use this if you have constant strings that should be stored in the source
|
|
|
|
language because they are exchanged over systems or users -- such as strings in
|
|
|
|
a database -- but should be translated at the last possible point in time, such
|
|
|
|
as when the string is presented to the user.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _lazy-translations:
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Lazy translation
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Use the function ``django.utils.translation.ugettext_lazy()`` to translate
|
2005-11-05 12:22:25 +08:00
|
|
|
strings lazily -- when the value is accessed rather than when the
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
``ugettext_lazy()`` function is called.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
For example, to translate a model's ``help_text``, do the following::
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-06-14 11:31:34 +08:00
|
|
|
class MyThing(models.Model):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
name = models.CharField(help_text=ugettext_lazy('This is the help text'))
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
In this example, ``ugettext_lazy()`` stores a lazy reference to the string --
|
2005-11-04 12:59:46 +08:00
|
|
|
not the actual translation. The translation itself will be done when the string
|
2008-10-06 16:32:35 +08:00
|
|
|
is used in a string context, such as template rendering on the Django admin
|
|
|
|
site.
|
|
|
|
|
|
|
|
The result of a ``ugettext_lazy()`` call can be used wherever you would use a
|
|
|
|
unicode string (an object with type ``unicode``) in Python. If you try to use
|
|
|
|
it where a bytestring (a ``str`` object) is expected, things will not work as
|
|
|
|
expected, since a ``ugettext_lazy()`` object doesn't know how to convert
|
|
|
|
itself to a bytestring. You can't use a unicode string inside a bytestring,
|
|
|
|
either, so this is consistent with normal Python behavior. For example::
|
|
|
|
|
|
|
|
# This is fine: putting a unicode proxy into a unicode string.
|
|
|
|
u"Hello %s" % ugettext_lazy("people")
|
|
|
|
|
|
|
|
# This will not work, since you cannot insert a unicode object
|
|
|
|
# into a bytestring (nor can you insert our unicode proxy there)
|
|
|
|
"Hello %s" % ugettext_lazy("people")
|
|
|
|
|
|
|
|
If you ever see output that looks like ``"hello
|
|
|
|
<django.utils.functional...>"``, you have tried to insert the result of
|
|
|
|
``ugettext_lazy()`` into a bytestring. That's a bug in your code.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
If you don't like the verbose name ``ugettext_lazy``, you can just alias it as
|
2005-11-05 12:40:07 +08:00
|
|
|
``_`` (underscore), like so::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-06-14 11:31:34 +08:00
|
|
|
class MyThing(models.Model):
|
|
|
|
name = models.CharField(help_text=_('This is the help text'))
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-09-11 11:28:49 +08:00
|
|
|
Always use lazy translations in :ref:`Django models <topics-db-models>`.
|
|
|
|
Field names and table names should be marked for translation (otherwise, they
|
|
|
|
won't be translated in the admin interface). This means writing explicit
|
2008-09-11 10:32:59 +08:00
|
|
|
``verbose_name`` and ``verbose_name_plural`` options in the ``Meta`` class,
|
2008-09-11 11:28:49 +08:00
|
|
|
though, rather than relying on Django's default determination of
|
|
|
|
``verbose_name`` and ``verbose_name_plural`` by looking at the model's class
|
|
|
|
name::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-06-14 11:31:34 +08:00
|
|
|
class MyThing(models.Model):
|
|
|
|
name = models.CharField(_('name'), help_text=_('This is the help text'))
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
2005-11-04 12:59:46 +08:00
|
|
|
verbose_name = _('my thing')
|
|
|
|
verbose_name_plural = _('mythings')
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Pluralization
|
|
|
|
~~~~~~~~~~~~~
|
2005-11-04 12:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Use the function ``django.utils.translation.ungettext()`` to specify pluralized
|
2009-07-03 13:42:09 +08:00
|
|
|
messages.
|
|
|
|
|
|
|
|
``ungettext`` takes three arguments: the singular translation string, the plural
|
|
|
|
translation string and the number of objects.
|
|
|
|
|
|
|
|
This function is useful when your need you Django application to be localizable
|
|
|
|
to languages where the number and complexity of `plural forms
|
|
|
|
<http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms>`_ is
|
|
|
|
greater than the two forms used in English ('object' for the singular and
|
|
|
|
'objects' for all the cases where ``count`` is different from zero, irrespective
|
|
|
|
of its value.)
|
|
|
|
|
|
|
|
For example::
|
2005-11-05 12:22:25 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ungettext
|
2005-11-04 12:59:46 +08:00
|
|
|
def hello_world(request, count):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
page = ungettext('there is %(count)d object', 'there are %(count)d objects', count) % {
|
2005-11-04 12:59:46 +08:00
|
|
|
'count': count,
|
|
|
|
}
|
|
|
|
return HttpResponse(page)
|
|
|
|
|
2009-07-03 13:42:09 +08:00
|
|
|
In this example the number of objects is passed to the translation languages as
|
|
|
|
the ``count`` variable.
|
|
|
|
|
|
|
|
Lets see a slightly more complex usage example::
|
|
|
|
|
|
|
|
from django.utils.translation import ungettext
|
|
|
|
|
|
|
|
count = Report.objects.count()
|
|
|
|
if count == 1:
|
|
|
|
name = Report._meta.verbose_name
|
|
|
|
else:
|
|
|
|
name = Report._meta.verbose_name_plural
|
|
|
|
|
|
|
|
text = ungettext(
|
|
|
|
'There is %(count)d %(name)s available.',
|
|
|
|
'There are %(count)d %(name)s available.',
|
|
|
|
count
|
|
|
|
) % {
|
|
|
|
'count': count,
|
|
|
|
'name': name
|
|
|
|
}
|
|
|
|
|
|
|
|
Here we reuse localizable, hopefully already translated literals (contained in
|
|
|
|
the ``verbose_name`` and ``verbose_name_plural`` model ``Meta`` options) for
|
|
|
|
other parts of the sentence so all of it is consistently based on the
|
|
|
|
cardinality of the elements at play.
|
|
|
|
|
|
|
|
.. _pluralization-var-notes:
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
When using this technique, make sure you use a single name for every
|
|
|
|
extrapolated variable included in the literal. In the example above note how
|
|
|
|
we used the ``name`` Python variable in both translation strings. This
|
|
|
|
example would fail::
|
|
|
|
|
|
|
|
from django.utils.translation import ungettext
|
|
|
|
from myapp.models import Report
|
|
|
|
|
|
|
|
count = Report.objects.count()
|
|
|
|
d = {
|
|
|
|
'count': count,
|
|
|
|
'name': Report._meta.verbose_name
|
|
|
|
'plural_name': Report._meta.verbose_name_plural
|
|
|
|
}
|
|
|
|
text = ungettext(
|
|
|
|
'There is %(count)d %(name)s available.',
|
|
|
|
'There are %(count)d %(plural_name)s available.',
|
|
|
|
count
|
|
|
|
) % d
|
|
|
|
|
|
|
|
You would get a ``a format specification for argument 'name', as in
|
|
|
|
'msgstr[0]', doesn't exist in 'msgid'`` error when running
|
|
|
|
``django-admin.py compilemessages`` or a ``KeyError`` Python exception at
|
|
|
|
runtime.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
In template code
|
|
|
|
----------------
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. highlightlang:: html+django
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Translations in :ref:`Django templates <topics-templates>` uses two template
|
|
|
|
tags and a slightly different syntax than in Python code. To give your template
|
|
|
|
access to these tags, put ``{% load i18n %}`` toward the top of your template.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
The ``{% trans %}`` template tag translates either a constant string
|
2008-08-26 03:28:41 +08:00
|
|
|
(enclosed in single or double quotes) or variable content::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
<title>{% trans "This is the title." %}</title>
|
2008-08-26 03:28:41 +08:00
|
|
|
<title>{% trans myvar %}</title>
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2009-03-24 19:44:51 +08:00
|
|
|
If the ``noop`` option is present, variable lookup still takes place, but the
|
|
|
|
original text will be returned unchanged. This is useful when "stubbing out"
|
|
|
|
content that will require translation in the future::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-08-26 03:28:41 +08:00
|
|
|
<title>{% trans "myvar" noop %}</title>
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2009-07-03 13:42:09 +08:00
|
|
|
Internally, inline translations use an ``ugettext`` call.
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
It's not possible to mix a template variable inside a string within ``{% trans
|
|
|
|
%}``. If your translations require strings with variables (placeholders), use
|
|
|
|
``{% blocktrans %}``::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-08-26 03:28:41 +08:00
|
|
|
{% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %}
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
To translate a template expression -- say, using template filters -- you need
|
|
|
|
to bind the expression to a local variable for use within the translation
|
|
|
|
block::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
{% blocktrans with value|filter as myvar %}
|
|
|
|
This will have {{ myvar }} inside.
|
2005-11-04 12:59:46 +08:00
|
|
|
{% endblocktrans %}
|
|
|
|
|
2006-07-14 10:57:10 +08:00
|
|
|
If you need to bind more than one expression inside a ``blocktrans`` tag,
|
|
|
|
separate the pieces with ``and``::
|
|
|
|
|
|
|
|
{% blocktrans with book|title as book_t and author|title as author_t %}
|
|
|
|
This is {{ book_t }} by {{ author_t }}
|
|
|
|
{% endblocktrans %}
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
To pluralize, specify both the singular and plural forms with the
|
|
|
|
``{% plural %}`` tag, which appears within ``{% blocktrans %}`` and
|
|
|
|
``{% endblocktrans %}``. Example::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2007-05-12 23:38:24 +08:00
|
|
|
{% blocktrans count list|length as counter %}
|
2005-11-04 12:59:46 +08:00
|
|
|
There is only one {{ name }} object.
|
|
|
|
{% plural %}
|
|
|
|
There are {{ counter }} {{ name }} objects.
|
|
|
|
{% endblocktrans %}
|
|
|
|
|
2009-07-03 13:42:09 +08:00
|
|
|
When you use the pluralization feature and bind additional values to local
|
|
|
|
variables apart from the counter value that selects the translated literal to be
|
|
|
|
used, have in mind that the ``blocktrans`` construct is internally converted
|
|
|
|
to an ``ungettext`` call. This means the same :ref:`notes regarding ungettext
|
|
|
|
variables <pluralization-var-notes>` apply.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2007-08-07 10:33:11 +08:00
|
|
|
Each ``RequestContext`` has access to three translation-specific variables:
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
* ``LANGUAGES`` is a list of tuples in which the first element is the
|
2008-07-06 12:08:13 +08:00
|
|
|
language code and the second is the language name (translated into the
|
|
|
|
currently active locale).
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
* ``LANGUAGE_CODE`` is the current user's preferred language, as a string.
|
2008-11-03 04:43:20 +08:00
|
|
|
Example: ``en-us``. (See :ref:`how-django-discovers-language-preference`,
|
|
|
|
below.)
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2008-07-06 12:08:13 +08:00
|
|
|
* ``LANGUAGE_BIDI`` is the current locale's direction. If True, it's a
|
2008-08-24 06:25:40 +08:00
|
|
|
right-to-left language, e.g.: Hebrew, Arabic. If False it's a
|
|
|
|
left-to-right language, e.g.: English, French, German etc.
|
2006-06-06 23:22:53 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
If you don't use the ``RequestContext`` extension, you can get those values with
|
2006-06-06 23:22:53 +08:00
|
|
|
three tags::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
{% get_current_language as LANGUAGE_CODE %}
|
|
|
|
{% get_available_languages as LANGUAGES %}
|
2006-06-06 23:22:53 +08:00
|
|
|
{% get_current_language_bidi as LANGUAGE_BIDI %}
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
These tags also require a ``{% load i18n %}``.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Translation hooks are also available within any template block tag that accepts
|
|
|
|
constant strings. In those cases, just use ``_()`` syntax to specify a
|
2008-09-04 04:52:20 +08:00
|
|
|
translation string::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
{% some_special_tag _("Page not found") value|yesno:_("yes,no") %}
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
In this case, both the tag and the filter will see the already-translated
|
|
|
|
string, so they don't need to be aware of translations.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-02-03 09:49:34 +08:00
|
|
|
.. note::
|
|
|
|
In this example, the translation infrastructure will be passed the string
|
|
|
|
``"yes,no"``, not the individual strings ``"yes"`` and ``"no"``. The
|
|
|
|
translated string will need to contain the comma so that the filter
|
|
|
|
parsing code knows how to split up the arguments. For example, a German
|
|
|
|
translator might translate the string ``"yes,no"`` as ``"ja,nein"``
|
|
|
|
(keeping the comma intact).
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _Django templates: ../templates_python/
|
2005-11-05 12:40:07 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Working with lazy translation objects
|
2007-09-16 01:32:53 +08:00
|
|
|
-------------------------------------
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. highlightlang:: python
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Using ``ugettext_lazy()`` and ``ungettext_lazy()`` to mark strings in models
|
|
|
|
and utility functions is a common operation. When you're working with these
|
|
|
|
objects elsewhere in your code, you should ensure that you don't accidentally
|
|
|
|
convert them to strings, because they should be converted as late as possible
|
|
|
|
(so that the correct locale is in effect). This necessitates the use of a
|
|
|
|
couple of helper functions.
|
|
|
|
|
|
|
|
Joining strings: string_concat()
|
2007-09-16 01:32:53 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Standard Python string joins (``''.join([...])``) will not work on lists
|
|
|
|
containing lazy translation objects. Instead, you can use
|
|
|
|
``django.utils.translation.string_concat()``, which creates a lazy object that
|
|
|
|
concatenates its contents *and* converts them to strings only when the result
|
|
|
|
is included in a string. For example::
|
|
|
|
|
|
|
|
from django.utils.translation import string_concat
|
|
|
|
...
|
|
|
|
name = ugettext_lazy(u'John Lennon')
|
|
|
|
instrument = ugettext_lazy(u'guitar')
|
|
|
|
result = string_concat([name, ': ', instrument])
|
|
|
|
|
|
|
|
In this case, the lazy translations in ``result`` will only be converted to
|
|
|
|
strings when ``result`` itself is used in a string (usually at template
|
|
|
|
rendering time).
|
|
|
|
|
|
|
|
The allow_lazy() decorator
|
2007-09-16 01:32:53 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
Django offers many utility functions (particularly in ``django.utils``) that
|
|
|
|
take a string as their first argument and do something to that string. These
|
|
|
|
functions are used by template filters as well as directly in other code.
|
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
If you write your own similar functions and deal with translations, you'll
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
face the problem of what to do when the first argument is a lazy translation
|
|
|
|
object. You don't want to convert it to a string immediately, because you might
|
|
|
|
be using this function outside of a view (and hence the current thread's locale
|
|
|
|
setting will not be correct).
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
For cases like this, use the ``django.utils.functional.allow_lazy()``
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
decorator. It modifies the function so that *if* it's called with a lazy
|
|
|
|
translation as the first argument, the function evaluation is delayed until it
|
|
|
|
needs to be converted to a string.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
from django.utils.functional import allow_lazy
|
|
|
|
|
|
|
|
def fancy_utility_function(s, ...):
|
|
|
|
# Do some conversion on string 's'
|
|
|
|
...
|
|
|
|
fancy_utility_function = allow_lazy(fancy_utility_function, unicode)
|
|
|
|
|
|
|
|
The ``allow_lazy()`` decorator takes, in addition to the function to decorate,
|
|
|
|
a number of extra arguments (``*args``) specifying the type(s) that the
|
|
|
|
original function can return. Usually, it's enough to include ``unicode`` here
|
|
|
|
and ensure that your function returns only Unicode strings.
|
|
|
|
|
|
|
|
Using this decorator means you can write your function and assume that the
|
|
|
|
input is a proper string, then add support for lazy translation objects at the
|
|
|
|
end.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _how-to-create-language-files:
|
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
2. How to create language files
|
|
|
|
===============================
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Once you've tagged your strings for later translation, you need to write (or
|
|
|
|
obtain) the language translations themselves. Here's how that works.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2007-03-12 17:02:18 +08:00
|
|
|
.. admonition:: Locale restrictions
|
|
|
|
|
2007-03-16 23:23:28 +08:00
|
|
|
Django does not support localizing your application into a locale for
|
|
|
|
which Django itself has not been translated. In this case, it will ignore
|
|
|
|
your translation files. If you were to try this and Django supported it,
|
|
|
|
you would inevitably see a mixture of translated strings (from your
|
|
|
|
application) and English strings (from Django itself). If you want to
|
|
|
|
support a locale for your application that is not already part of
|
|
|
|
Django, you'll need to make at least a minimal translation of the Django
|
2008-09-09 09:54:20 +08:00
|
|
|
core. See the relevant :ref:`LocaleMiddleware note<locale-middleware-notes>`
|
2008-09-04 04:52:20 +08:00
|
|
|
for more details.
|
2007-03-12 17:02:18 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
Message files
|
|
|
|
-------------
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
The first step is to create a **message file** for a new language. A message
|
|
|
|
file is a plain-text file, representing a single language, that contains all
|
|
|
|
available translation strings and how they should be represented in the given
|
|
|
|
language. Message files have a ``.po`` file extension.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
Django comes with a tool, ``django-admin.py makemessages``, that automates the
|
2008-08-24 06:25:40 +08:00
|
|
|
creation and upkeep of these files.
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
.. admonition:: A note to Django veterans
|
|
|
|
|
|
|
|
The old tool ``bin/make-messages.py`` has been moved to the command
|
|
|
|
``django-admin.py makemessages`` to provide consistency throughout Django.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
To create or update a message file, run this command::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
django-admin.py makemessages -l de
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
...where ``de`` is the language code for the message file you want to create.
|
2005-11-05 12:40:07 +08:00
|
|
|
The language code, in this case, is in locale format. For example, it's
|
2007-09-16 01:32:53 +08:00
|
|
|
``pt_BR`` for Brazilian Portuguese and ``de_AT`` for Austrian German.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:40:07 +08:00
|
|
|
The script should be run from one of three places:
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
* The root directory of your Django project.
|
|
|
|
* The root directory of your Django app.
|
2008-09-04 04:52:20 +08:00
|
|
|
* The root ``django`` directory (not a Subversion checkout, but the one
|
|
|
|
that is linked-to via ``$PYTHONPATH`` or is located somewhere on that
|
|
|
|
path). This is only relevant when you are creating a translation for
|
|
|
|
Django itself, see :ref:`contributing-translations`.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2009-04-16 20:45:04 +08:00
|
|
|
The script runs over your project source tree or your application source tree
|
|
|
|
and pulls out all strings marked for translation. It creates (or updates) a
|
|
|
|
message file in the directory ``locale/LANG/LC_MESSAGES``. In the ``de``
|
|
|
|
example, the file will be ``locale/de/LC_MESSAGES/django.po``.
|
2006-01-07 18:21:51 +08:00
|
|
|
|
2008-08-09 00:41:55 +08:00
|
|
|
By default ``django-admin.py makemessages`` examines every file that has the
|
|
|
|
``.html`` file extension. In case you want to override that default, use the
|
|
|
|
``--extension`` or ``-e`` option to specify the file extensions to examine::
|
|
|
|
|
|
|
|
django-admin.py makemessages -l de -e txt
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
Separate multiple extensions with commas and/or use ``-e`` or ``--extension``
|
|
|
|
multiple times::
|
2008-08-09 00:41:55 +08:00
|
|
|
|
|
|
|
django-admin.py makemessages -l=de -e=html,txt -e xml
|
|
|
|
|
|
|
|
When `creating JavaScript translation catalogs`_ you need to use the special
|
|
|
|
'djangojs' domain, **not** ``-e js``.
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. _create a JavaScript translation catalog: `Creating JavaScript translation catalogs`_
|
2008-08-09 00:41:55 +08:00
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
.. admonition:: No gettext?
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
If you don't have the ``gettext`` utilities installed, ``django-admin.py
|
|
|
|
makemessages`` will create empty files. If that's the case, either install
|
|
|
|
the ``gettext`` utilities or just copy the English message file
|
|
|
|
(``locale/en/LC_MESSAGES/django.po``) if available and use it as a starting
|
|
|
|
point; it's just an empty translation file.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2008-07-29 14:02:47 +08:00
|
|
|
.. admonition:: Working on Windows?
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
If you're using Windows and need to install the GNU gettext utilities so
|
2008-07-29 14:02:47 +08:00
|
|
|
``django-admin makemessages`` works see `gettext on Windows`_ for more
|
|
|
|
information.
|
|
|
|
|
2005-11-05 12:22:25 +08:00
|
|
|
The format of ``.po`` files is straightforward. Each ``.po`` file contains a
|
|
|
|
small bit of metadata, such as the translation maintainer's contact
|
|
|
|
information, but the bulk of the file is a list of **messages** -- simple
|
|
|
|
mappings between translation strings and the actual translated text for the
|
|
|
|
particular language.
|
|
|
|
|
|
|
|
For example, if your Django app contained a translation string for the text
|
2005-11-05 12:24:19 +08:00
|
|
|
``"Welcome to my site."``, like so::
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
_("Welcome to my site.")
|
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
...then ``django-admin.py makemessages`` will have created a ``.po`` file
|
|
|
|
containing the following snippet -- a message::
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
#: path/to/python/module.py:23
|
|
|
|
msgid "Welcome to my site."
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
A quick explanation:
|
|
|
|
|
|
|
|
* ``msgid`` is the translation string, which appears in the source. Don't
|
|
|
|
change it.
|
|
|
|
* ``msgstr`` is where you put the language-specific translation. It starts
|
|
|
|
out empty, so it's your responsibility to change it. Make sure you keep
|
|
|
|
the quotes around your translation.
|
2008-09-04 04:52:20 +08:00
|
|
|
* As a convenience, each message includes, in the form of a comment line
|
2008-11-03 04:43:20 +08:00
|
|
|
prefixed with ``#`` and located above the ``msgid`` line, the filename and
|
2008-09-04 04:52:20 +08:00
|
|
|
line number from which the translation string was gleaned.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
Long messages are a special case. There, the first string directly after the
|
|
|
|
``msgstr`` (or ``msgid``) is an empty string. Then the content itself will be
|
|
|
|
written over the next few lines as one string per line. Those strings are
|
2006-07-14 10:57:10 +08:00
|
|
|
directly concatenated. Don't forget trailing spaces within the strings;
|
2005-11-05 12:22:25 +08:00
|
|
|
otherwise, they'll be tacked together without whitespace!
|
|
|
|
|
|
|
|
.. admonition:: Mind your charset
|
|
|
|
|
2007-10-08 00:35:22 +08:00
|
|
|
When creating a PO file with your favorite text editor, first edit
|
2005-11-05 12:22:25 +08:00
|
|
|
the charset line (search for ``"CHARSET"``) and set it to the charset
|
2007-10-08 00:35:22 +08:00
|
|
|
you'll be using to edit the content. Due to the way the ``gettext`` tools
|
|
|
|
work internally and because we want to allow non-ASCII source strings in
|
|
|
|
Django's core and your applications, you **must** use UTF-8 as the encoding
|
2007-11-30 13:08:26 +08:00
|
|
|
for your PO file. This means that everybody will be using the same
|
|
|
|
encoding, which is important when Django processes the PO files.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
To reexamine all source code and templates for new translation strings and
|
2005-11-05 12:40:07 +08:00
|
|
|
update all message files for **all** languages, run this::
|
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
django-admin.py makemessages -a
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
Compiling message files
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
After you create your message file -- and each time you make changes to it --
|
|
|
|
you'll need to compile it into a more efficient form, for use by ``gettext``.
|
2008-07-06 14:39:44 +08:00
|
|
|
Do this with the ``django-admin.py compilemessages`` utility.
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
This tool runs over all available ``.po`` files and creates ``.mo`` files, which
|
|
|
|
are binary files optimized for use by ``gettext``. In the same directory from
|
|
|
|
which you ran ``django-admin.py makemessages``, run ``django-admin.py
|
|
|
|
compilemessages`` like this::
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
django-admin.py compilemessages
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
That's it. Your translations are ready for use.
|
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
.. admonition:: A note to Django veterans
|
|
|
|
|
|
|
|
The old tool ``bin/compile-messages.py`` has been moved to the command
|
|
|
|
``django-admin.py compilemessages`` to provide consistency throughout
|
|
|
|
Django.
|
|
|
|
|
2008-07-29 14:02:47 +08:00
|
|
|
.. admonition:: Working on Windows?
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
If you're using Windows and need to install the GNU gettext utilities so
|
2008-07-29 14:02:47 +08:00
|
|
|
``django-admin compilemessages`` works see `gettext on Windows`_ for more
|
|
|
|
information.
|
|
|
|
|
2008-11-03 04:43:20 +08:00
|
|
|
.. _how-django-discovers-language-preference:
|
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
3. How Django discovers language preference
|
|
|
|
===========================================
|
2005-11-05 12:22:25 +08:00
|
|
|
|
|
|
|
Once you've prepared your translations -- or, if you just want to use the
|
|
|
|
translations that come with Django -- you'll just need to activate translation
|
|
|
|
for your app.
|
|
|
|
|
|
|
|
Behind the scenes, Django has a very flexible model of deciding which language
|
|
|
|
should be used -- installation-wide, for a particular user, or both.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
To set an installation-wide language preference, set :setting:`LANGUAGE_CODE`.
|
|
|
|
Django uses this language as the default translation -- the final attempt if no
|
|
|
|
other translator finds a translation.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
If all you want to do is run Django with your native language, and a language
|
|
|
|
file is available for your language, all you need to do is set
|
|
|
|
``LANGUAGE_CODE``.
|
|
|
|
|
|
|
|
If you want to let each individual user specify which language he or she
|
|
|
|
prefers, use ``LocaleMiddleware``. ``LocaleMiddleware`` enables language
|
2005-11-05 12:22:25 +08:00
|
|
|
selection based on data from the request. It customizes content for each user.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
To use ``LocaleMiddleware``, add ``'django.middleware.locale.LocaleMiddleware'``
|
|
|
|
to your ``MIDDLEWARE_CLASSES`` setting. Because middleware order matters, you
|
|
|
|
should follow these guidelines:
|
|
|
|
|
|
|
|
* Make sure it's one of the first middlewares installed.
|
|
|
|
* It should come after ``SessionMiddleware``, because ``LocaleMiddleware``
|
|
|
|
makes use of session data.
|
2005-11-07 07:57:56 +08:00
|
|
|
* If you use ``CacheMiddleware``, put ``LocaleMiddleware`` after it.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
For example, your ``MIDDLEWARE_CLASSES`` might look like this::
|
|
|
|
|
|
|
|
MIDDLEWARE_CLASSES = (
|
2006-05-02 09:31:56 +08:00
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
2005-11-04 12:59:46 +08:00
|
|
|
'django.middleware.locale.LocaleMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
)
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
(For more on middleware, see the :ref:`middleware documentation
|
|
|
|
<topics-http-middleware>`.)
|
2005-11-05 12:22:25 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
``LocaleMiddleware`` tries to determine the user's language preference by
|
|
|
|
following this algorithm:
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
* First, it looks for a ``django_language`` key in the current user's
|
|
|
|
session.
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
* Failing that, it looks for a cookie.
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. versionchanged:: 1.0
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
In Django version 0.96 and before, the cookie's name is hard-coded to
|
|
|
|
``django_language``. In Django 1,0, The cookie name is set by the
|
|
|
|
``LANGUAGE_COOKIE_NAME`` setting. (The default name is
|
|
|
|
``django_language``.)
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
* Failing that, it looks at the ``Accept-Language`` HTTP header. This
|
|
|
|
header is sent by your browser and tells the server which language(s) you
|
|
|
|
prefer, in order by priority. Django tries each language in the header
|
|
|
|
until it finds one with available translations.
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
* Failing that, it uses the global ``LANGUAGE_CODE`` setting.
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
.. _locale-middleware-notes:
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
Notes:
|
|
|
|
|
|
|
|
* In each of these places, the language preference is expected to be in the
|
2007-09-16 01:32:53 +08:00
|
|
|
standard language format, as a string. For example, Brazilian Portuguese
|
2007-05-08 10:25:02 +08:00
|
|
|
is ``pt-br``.
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
* If a base language is available but the sublanguage specified is not,
|
|
|
|
Django uses the base language. For example, if a user specifies ``de-at``
|
|
|
|
(Austrian German) but Django only has ``de`` available, Django uses
|
|
|
|
``de``.
|
2008-09-11 11:28:49 +08:00
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
* Only languages listed in the :setting:`LANGUAGES` setting can be selected.
|
|
|
|
If you want to restrict the language selection to a subset of provided
|
2006-07-11 10:49:56 +08:00
|
|
|
languages (because your application doesn't provide all those languages),
|
2005-11-13 08:19:16 +08:00
|
|
|
set ``LANGUAGES`` to a list of languages. For example::
|
2005-11-13 05:33:46 +08:00
|
|
|
|
|
|
|
LANGUAGES = (
|
|
|
|
('de', _('German')),
|
|
|
|
('en', _('English')),
|
|
|
|
)
|
|
|
|
|
2005-11-13 08:19:16 +08:00
|
|
|
This example restricts languages that are available for automatic
|
|
|
|
selection to German and English (and any sublanguage, like de-ch or
|
|
|
|
en-us).
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _LANGUAGES setting: ../settings/#languages
|
2006-07-11 10:49:56 +08:00
|
|
|
|
|
|
|
* If you define a custom ``LANGUAGES`` setting, as explained in the
|
|
|
|
previous bullet, it's OK to mark the languages as translation strings
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
-- but use a "dummy" ``ugettext()`` function, not the one in
|
2006-07-11 10:49:56 +08:00
|
|
|
``django.utils.translation``. You should *never* import
|
|
|
|
``django.utils.translation`` from within your settings file, because that
|
|
|
|
module in itself depends on the settings, and that would cause a circular
|
|
|
|
import.
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
The solution is to use a "dummy" ``ugettext()`` function. Here's a sample
|
2006-07-11 10:49:56 +08:00
|
|
|
settings file::
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
ugettext = lambda s: s
|
2006-07-11 10:49:56 +08:00
|
|
|
|
|
|
|
LANGUAGES = (
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
('de', ugettext('German')),
|
|
|
|
('en', ugettext('English')),
|
2006-07-11 10:49:56 +08:00
|
|
|
)
|
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
With this arrangement, ``django-admin.py makemessages`` will still find
|
|
|
|
and mark these strings for translation, but the translation won't happen
|
2008-09-04 04:52:20 +08:00
|
|
|
at runtime -- so you'll have to remember to wrap the languages in the
|
|
|
|
*real* ``ugettext()`` in any code that uses ``LANGUAGES`` at runtime.
|
2006-07-11 10:49:56 +08:00
|
|
|
|
2006-01-19 09:08:42 +08:00
|
|
|
* The ``LocaleMiddleware`` can only select languages for which there is a
|
|
|
|
Django-provided base translation. If you want to provide translations
|
2006-01-18 21:01:43 +08:00
|
|
|
for your application that aren't already in the set of translations
|
2006-01-19 09:08:42 +08:00
|
|
|
in Django's source tree, you'll want to provide at least basic
|
|
|
|
translations for that language. For example, Django uses technical
|
|
|
|
message IDs to translate date formats and time formats -- so you will
|
2006-01-18 21:01:43 +08:00
|
|
|
need at least those translations for the system to work correctly.
|
|
|
|
|
2006-01-19 09:08:42 +08:00
|
|
|
A good starting point is to copy the English ``.po`` file and to
|
2008-09-04 04:52:20 +08:00
|
|
|
translate at least the technical messages -- maybe the validation
|
2006-01-18 21:01:43 +08:00
|
|
|
messages, too.
|
2006-01-19 09:08:42 +08:00
|
|
|
|
|
|
|
Technical message IDs are easily recognized; they're all upper case. You
|
|
|
|
don't translate the message ID as with other messages, you provide the
|
|
|
|
correct local variant on the provided English value. For example, with
|
2006-01-18 21:01:43 +08:00
|
|
|
``DATETIME_FORMAT`` (or ``DATE_FORMAT`` or ``TIME_FORMAT``), this would
|
2006-01-19 09:08:42 +08:00
|
|
|
be the format string that you want to use in your language. The format
|
|
|
|
is identical to the format strings used by the ``now`` template tag.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
Once ``LocaleMiddleware`` determines the user's preference, it makes this
|
2008-08-24 06:25:40 +08:00
|
|
|
preference available as ``request.LANGUAGE_CODE`` for each
|
|
|
|
:class:`~django.http.HttpRequest`. Feel free to read this value in your view
|
|
|
|
code. Here's a simple example::
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
def hello_world(request, count):
|
|
|
|
if request.LANGUAGE_CODE == 'de-at':
|
|
|
|
return HttpResponse("You prefer to read Austrian German.")
|
|
|
|
else:
|
|
|
|
return HttpResponse("You prefer to read another language.")
|
|
|
|
|
|
|
|
Note that, with static (middleware-less) translation, the language is in
|
|
|
|
``settings.LANGUAGE_CODE``, while with dynamic (middleware) translation, it's
|
|
|
|
in ``request.LANGUAGE_CODE``.
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _settings file: ../settings/
|
|
|
|
.. _middleware documentation: ../middleware/
|
|
|
|
.. _session: ../sessions/
|
|
|
|
.. _request object: ../request_response/#httprequest-objects
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _translations-in-your-own-projects:
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
Using translations in your own projects
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
Django looks for translations by following this algorithm:
|
|
|
|
|
|
|
|
* First, it looks for a ``locale`` directory in the application directory
|
|
|
|
of the view that's being called. If it finds a translation for the
|
|
|
|
selected language, the translation will be installed.
|
|
|
|
* Next, it looks for a ``locale`` directory in the project directory. If it
|
|
|
|
finds a translation, the translation will be installed.
|
2008-09-04 04:52:20 +08:00
|
|
|
* Finally, it checks the Django-provided base translation in
|
|
|
|
``django/conf/locale``.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
This way, you can write applications that include their own translations, and
|
2005-11-05 12:22:25 +08:00
|
|
|
you can override base translations in your project path. Or, you can just build
|
|
|
|
a big project out of several apps and put all translations into one big project
|
|
|
|
message file. The choice is yours.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
.. note::
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
If you're using manually configured settings, as described
|
|
|
|
:ref:`settings-without-django-settings-module`, the ``locale`` directory in
|
|
|
|
the project directory will not be examined, since Django loses the ability
|
|
|
|
to work out the location of the project directory. (Django normally uses the
|
|
|
|
location of the settings file to determine this, and a settings file doesn't
|
|
|
|
exist if you're manually configuring your settings.)
|
2006-05-17 05:28:06 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
All message file repositories are structured the same way. They are:
|
|
|
|
|
|
|
|
* ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
|
|
|
|
* ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)``
|
2005-11-05 12:22:25 +08:00
|
|
|
* All paths listed in ``LOCALE_PATHS`` in your settings file are
|
2005-11-04 12:59:46 +08:00
|
|
|
searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)``
|
|
|
|
* ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)``
|
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
To create message files, you use the same ``django-admin.py makemessages``
|
|
|
|
tool as with the Django message files. You only need to be in the right place
|
|
|
|
-- in the directory where either the ``conf/locale`` (in case of the source
|
|
|
|
tree) or the ``locale/`` (in case of app messages or project messages)
|
|
|
|
directory are located. And you use the same ``django-admin.py compilemessages``
|
|
|
|
to produce the binary ``django.mo`` files that are used by ``gettext``.
|
2007-09-16 11:17:48 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
You can also run ``django-admin.py compilemessages --settings=path.to.settings``
|
|
|
|
to make the compiler process all the directories in your ``LOCALE_PATHS``
|
|
|
|
setting.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
Application message files are a bit complicated to discover -- they need the
|
|
|
|
``LocaleMiddleware``. If you don't use the middleware, only the Django message
|
|
|
|
files and project message files will be processed.
|
|
|
|
|
|
|
|
Finally, you should give some thought to the structure of your translation
|
|
|
|
files. If your applications need to be delivered to other users and will
|
|
|
|
be used in other projects, you might want to use app-specific translations.
|
|
|
|
But using app-specific translations and project translations could produce
|
2008-07-06 14:39:44 +08:00
|
|
|
weird problems with ``makemessages``: ``makemessages`` will traverse all
|
2005-11-05 12:22:25 +08:00
|
|
|
directories below the current path and so might put message IDs into the
|
|
|
|
project message file that are already in application message files.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
The easiest way out is to store applications that are not part of the project
|
|
|
|
(and so carry their own translations) outside the project tree. That way,
|
2008-07-06 14:39:44 +08:00
|
|
|
``django-admin.py makemessages`` on the project level will only translate
|
|
|
|
strings that are connected to your explicit project and not strings that are
|
|
|
|
distributed independently.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
The ``set_language`` redirect view
|
|
|
|
==================================
|
|
|
|
|
|
|
|
As a convenience, Django comes with a view, ``django.views.i18n.set_language``,
|
|
|
|
that sets a user's language preference and redirects back to the previous page.
|
|
|
|
|
|
|
|
Activate this view by adding the following line to your URLconf::
|
|
|
|
|
|
|
|
(r'^i18n/', include('django.conf.urls.i18n')),
|
|
|
|
|
|
|
|
(Note that this example makes the view available at ``/i18n/setlang/``.)
|
|
|
|
|
|
|
|
The view expects to be called via the ``POST`` method, with a ``language``
|
|
|
|
parameter set in request. If session support is enabled, the view
|
|
|
|
saves the language choice in the user's session. Otherwise, it saves the
|
2008-03-14 04:26:08 +08:00
|
|
|
language choice in a cookie that is by default named ``django_language``.
|
2008-11-15 13:51:25 +08:00
|
|
|
(The name can be changed through the ``LANGUAGE_COOKIE_NAME`` setting.)
|
2007-09-16 01:32:53 +08:00
|
|
|
|
|
|
|
After setting the language choice, Django redirects the user, following this
|
|
|
|
algorithm:
|
|
|
|
|
2007-09-16 06:36:43 +08:00
|
|
|
* Django looks for a ``next`` parameter in the ``POST`` data.
|
2007-09-16 01:32:53 +08:00
|
|
|
* If that doesn't exist, or is empty, Django tries the URL in the
|
|
|
|
``Referrer`` header.
|
|
|
|
* If that's empty -- say, if a user's browser suppresses that header --
|
|
|
|
then the user will be redirected to ``/`` (the site root) as a fallback.
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
Here's example HTML template code:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-09-16 01:32:53 +08:00
|
|
|
|
|
|
|
<form action="/i18n/setlang/" method="post">
|
|
|
|
<input name="next" type="hidden" value="/next/page/" />
|
|
|
|
<select name="language">
|
|
|
|
{% for lang in LANGUAGES %}
|
|
|
|
<option value="{{ lang.0 }}">{{ lang.1 }}</option>
|
|
|
|
{% endfor %}
|
|
|
|
</select>
|
|
|
|
<input type="submit" value="Go" />
|
|
|
|
</form>
|
|
|
|
|
2005-12-04 21:08:37 +08:00
|
|
|
Translations and JavaScript
|
|
|
|
===========================
|
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
Adding translations to JavaScript poses some problems:
|
|
|
|
|
|
|
|
* JavaScript code doesn't have access to a ``gettext`` implementation.
|
|
|
|
|
|
|
|
* JavaScript code doesn't have access to .po or .mo files; they need to be
|
|
|
|
delivered by the server.
|
|
|
|
|
|
|
|
* The translation catalogs for JavaScript should be kept as small as
|
|
|
|
possible.
|
|
|
|
|
|
|
|
Django provides an integrated solution for these problems: It passes the
|
|
|
|
translations into JavaScript, so you can call ``gettext``, etc., from within
|
|
|
|
JavaScript.
|
|
|
|
|
|
|
|
The ``javascript_catalog`` view
|
|
|
|
-------------------------------
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
The main solution to these problems is the ``javascript_catalog`` view, which
|
2006-07-14 10:57:10 +08:00
|
|
|
sends out a JavaScript code library with functions that mimic the ``gettext``
|
2005-12-05 03:50:12 +08:00
|
|
|
interface, plus an array of translation strings. Those translation strings are
|
|
|
|
taken from the application, project or Django core, according to what you
|
2007-09-15 22:46:41 +08:00
|
|
|
specify in either the info_dict or the URL.
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
You hook it up like this::
|
|
|
|
|
|
|
|
js_info_dict = {
|
|
|
|
'packages': ('your.app.package',),
|
|
|
|
}
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
|
|
|
|
)
|
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
Each string in ``packages`` should be in Python dotted-package syntax (the
|
|
|
|
same format as the strings in ``INSTALLED_APPS``) and should refer to a package
|
|
|
|
that contains a ``locale`` directory. If you specify multiple packages, all
|
2006-07-14 10:57:10 +08:00
|
|
|
those catalogs are merged into one catalog. This is useful if you have
|
2005-12-05 03:50:12 +08:00
|
|
|
JavaScript that uses strings from different applications.
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
You can make the view dynamic by putting the packages into the URL pattern::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
2008-03-24 21:35:27 +08:00
|
|
|
(r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
|
2005-12-04 21:08:37 +08:00
|
|
|
)
|
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
With this, you specify the packages as a list of package names delimited by '+'
|
|
|
|
signs in the URL. This is especially useful if your pages use code from
|
|
|
|
different apps and this changes often and you don't want to pull in one big
|
|
|
|
catalog file. As a security measure, these values can only be either
|
|
|
|
``django.conf`` or any package from the ``INSTALLED_APPS`` setting.
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
Using the JavaScript translation catalog
|
|
|
|
----------------------------------------
|
|
|
|
|
2005-12-05 03:50:12 +08:00
|
|
|
To use the catalog, just pull in the dynamically generated script like this::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2009-07-15 21:54:45 +08:00
|
|
|
<script type="text/javascript" src="{% url django.views.i18n.javascript_catalog %}"></script>
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2009-07-15 21:54:45 +08:00
|
|
|
This uses reverse URL lookup to find the URL of the JavaScript catalog view.
|
|
|
|
When the catalog is loaded, your JavaScript code can use the standard
|
|
|
|
``gettext`` interface to access it::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
document.write(gettext('this is to be translated'));
|
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
There is also an ``ngettext`` interface::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
var object_cnt = 1 // or 0, or 2, or 3, ...
|
|
|
|
s = ngettext('literal for the singular case',
|
|
|
|
'literal for the plural case', object_cnt);
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
and even a string interpolation function::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
function interpolate(fmt, obj, named);
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2008-03-24 21:35:27 +08:00
|
|
|
The interpolation syntax is borrowed from Python, so the ``interpolate``
|
|
|
|
function supports both positional and named interpolation:
|
|
|
|
|
|
|
|
* Positional interpolation: ``obj`` contains a JavaScript Array object
|
2008-03-25 12:42:21 +08:00
|
|
|
whose elements values are then sequentially interpolated in their
|
2008-03-24 21:35:27 +08:00
|
|
|
corresponding ``fmt`` placeholders in the same order they appear.
|
|
|
|
For example::
|
|
|
|
|
|
|
|
fmts = ngettext('There is %s object. Remaining: %s',
|
|
|
|
'There are %s objects. Remaining: %s', 11);
|
|
|
|
s = interpolate(fmts, [11, 20]);
|
2008-03-25 12:42:21 +08:00
|
|
|
// s is 'There are 11 objects. Remaining: 20'
|
2008-03-24 21:35:27 +08:00
|
|
|
|
|
|
|
* Named interpolation: This mode is selected by passing the optional
|
|
|
|
boolean ``named`` parameter as true. ``obj`` contains a JavaScript
|
|
|
|
object or associative array. For example::
|
|
|
|
|
|
|
|
d = {
|
|
|
|
count: 10
|
|
|
|
total: 50
|
|
|
|
};
|
|
|
|
|
|
|
|
fmts = ngettext('Total: %(total)s, there is %(count)s object',
|
2008-03-25 12:42:21 +08:00
|
|
|
'there are %(count)s of a total of %(total)s objects', d.count);
|
2008-03-24 21:35:27 +08:00
|
|
|
s = interpolate(fmts, d, true);
|
|
|
|
|
|
|
|
You shouldn't go over the top with string interpolation, though: this is still
|
|
|
|
JavaScript, so the code has to make repeated regular-expression substitutions.
|
|
|
|
This isn't as fast as string interpolation in Python, so keep it to those
|
|
|
|
cases where you really need it (for example, in conjunction with ``ngettext``
|
|
|
|
to produce proper pluralizations).
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
Creating JavaScript translation catalogs
|
|
|
|
----------------------------------------
|
|
|
|
|
2007-09-15 22:46:41 +08:00
|
|
|
You create and update the translation catalogs the same way as the other
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
Django translation catalogs -- with the django-admin.py makemessages tool. The
|
|
|
|
only difference is you need to provide a ``-d djangojs`` parameter, like this::
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2008-07-06 14:39:44 +08:00
|
|
|
django-admin.py makemessages -d djangojs -l de
|
2005-12-04 21:08:37 +08:00
|
|
|
|
|
|
|
This would create or update the translation catalog for JavaScript for German.
|
2008-07-06 14:39:44 +08:00
|
|
|
After updating translation catalogs, just run ``django-admin.py compilemessages``
|
|
|
|
the same way as you do with normal Django translation catalogs.
|
2005-12-04 21:08:37 +08:00
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
Specialties of Django translation
|
2005-11-04 12:59:46 +08:00
|
|
|
==================================
|
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
If you know ``gettext``, you might note these specialties in the way Django
|
2005-11-04 12:59:46 +08:00
|
|
|
does translation:
|
|
|
|
|
2007-09-16 01:32:53 +08:00
|
|
|
* The string domain is ``django`` or ``djangojs``. This string domain is
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
used to differentiate between different programs that store their data
|
|
|
|
in a common message-file library (usually ``/usr/share/locale/``). The
|
2008-08-24 06:25:40 +08:00
|
|
|
``django`` domain is used for python and template translation strings
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
and is loaded into the global translation catalogs. The ``djangojs``
|
|
|
|
domain is only used for JavaScript translation catalogs to make sure
|
|
|
|
that those are as small as possible.
|
2005-11-04 12:59:46 +08:00
|
|
|
* Django doesn't use ``xgettext`` alone. It uses Python wrappers around
|
2007-09-16 01:32:53 +08:00
|
|
|
``xgettext`` and ``msgfmt``. This is mostly for convenience.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2008-07-29 14:02:47 +08:00
|
|
|
``gettext`` on Windows
|
|
|
|
======================
|
|
|
|
|
2008-09-04 04:52:20 +08:00
|
|
|
This is only needed for people who either want to extract message IDs or compile
|
|
|
|
message files (``.po``). Translation work itself just involves editing existing
|
|
|
|
files of this type, but if you want to create your own message files, or want to
|
|
|
|
test or compile a changed message file, you will need the ``gettext`` utilities:
|
2008-07-29 14:02:47 +08:00
|
|
|
|
2009-06-25 07:33:17 +08:00
|
|
|
* Download the following zip files from the GNOME servers
|
|
|
|
http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ or from one
|
|
|
|
of its mirrors_
|
2008-07-29 14:02:47 +08:00
|
|
|
|
2009-06-25 07:33:17 +08:00
|
|
|
* ``gettext-runtime-X.zip``
|
|
|
|
* ``gettext-tools-X.zip``
|
2008-07-29 14:02:47 +08:00
|
|
|
|
2009-06-25 07:33:17 +08:00
|
|
|
``X`` is the version number, we recomend using ``0.15`` or higher.
|
|
|
|
|
|
|
|
* Extract the contents of the ``bin\`` directories in both files to the
|
|
|
|
same folder on your system (i.e. ``C:\Program Files\gettext-utils``)
|
2008-07-29 14:02:47 +08:00
|
|
|
|
|
|
|
* Update the system PATH:
|
|
|
|
|
|
|
|
* ``Control Panel > System > Advanced > Environment Variables``
|
|
|
|
* In the ``System variables`` list, click ``Path``, click ``Edit``
|
2008-09-04 04:52:20 +08:00
|
|
|
* Add ``;C:\Program Files\gettext-utils\bin`` at the end of the
|
|
|
|
``Variable value`` field
|
2008-10-06 09:36:35 +08:00
|
|
|
|
2009-06-25 07:33:17 +08:00
|
|
|
.. _mirrors: http://ftp.gnome.org/pub/GNOME/MIRRORS
|
|
|
|
|
2009-03-24 19:44:51 +08:00
|
|
|
You may also use ``gettext`` binaries you have obtained elsewhere, so long as
|
2008-10-06 09:36:35 +08:00
|
|
|
the ``xgettext --version`` command works properly. Some version 0.14.4 binaries
|
2009-03-24 19:44:51 +08:00
|
|
|
have been found to not support this command. Do not attempt to use Django
|
2008-10-06 09:36:35 +08:00
|
|
|
translation utilities with a ``gettext`` package if the command ``xgettext
|
|
|
|
--version`` entered at a Windows command prompt causes a popup window saying
|
|
|
|
"xgettext.exe has generated errors and will be closed by Windows".
|