Add missing imports and models to the examples in internationalization and localization documentation

This commit is contained in:
Silvan Spross 2013-05-19 12:42:44 +02:00
parent 6a479955f0
commit 1d543949d7
1 changed files with 15 additions and 2 deletions

View File

@ -80,6 +80,7 @@ In this example, the text ``"Welcome to my site."`` is marked as a translation
string:: string::
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.http import HttpResponse
def my_view(request): def my_view(request):
output = _("Welcome to my site.") output = _("Welcome to my site.")
@ -89,6 +90,7 @@ Obviously, you could code this without using the alias. This example is
identical to the previous one:: identical to the previous one::
from django.utils.translation import ugettext from django.utils.translation import ugettext
from django.http import HttpResponse
def my_view(request): def my_view(request):
output = ugettext("Welcome to my site.") output = ugettext("Welcome to my site.")
@ -192,6 +194,7 @@ of its value.)
For example:: For example::
from django.utils.translation import ungettext from django.utils.translation import ungettext
from django.http import HttpResponse
def hello_world(request, count): def hello_world(request, count):
page = ungettext( page = ungettext(
@ -208,6 +211,7 @@ languages as the ``count`` variable.
Lets see a slightly more complex usage example:: Lets see a slightly more complex usage example::
from django.utils.translation import ungettext from django.utils.translation import ungettext
from myapp.models import Report
count = Report.objects.count() count = Report.objects.count()
if count == 1: if count == 1:
@ -283,6 +287,7 @@ For example::
or:: or::
from django.db import models
from django.utils.translation import pgettext_lazy from django.utils.translation import pgettext_lazy
class MyThing(models.Model): class MyThing(models.Model):
@ -328,6 +333,7 @@ Model fields and relationships ``verbose_name`` and ``help_text`` option values
For example, to translate the help text of the *name* field in the following For example, to translate the help text of the *name* field in the following
model, do the following:: model, do the following::
from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model): class MyThing(models.Model):
@ -336,8 +342,6 @@ model, do the following::
You can mark names of ``ForeignKey``, ``ManyTomanyField`` or ``OneToOneField`` You can mark names of ``ForeignKey``, ``ManyTomanyField`` or ``OneToOneField``
relationship as translatable by using their ``verbose_name`` options:: relationship as translatable by using their ``verbose_name`` options::
from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model): class MyThing(models.Model):
kind = models.ForeignKey(ThingKind, related_name='kinds', kind = models.ForeignKey(ThingKind, related_name='kinds',
verbose_name=_('kind')) verbose_name=_('kind'))
@ -355,6 +359,7 @@ It is recommended to always provide explicit
relying on the fallback English-centric and somewhat naïve determination of relying on the fallback English-centric and somewhat naïve determination of
verbose names Django performs by looking at the model's class name:: verbose names Django performs by looking at the model's class name::
from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model): class MyThing(models.Model):
@ -370,6 +375,7 @@ Model methods ``short_description`` attribute values
For model methods, you can provide translations to Django and the admin site For model methods, you can provide translations to Django and the admin site
with the ``short_description`` attribute:: with the ``short_description`` attribute::
from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model): class MyThing(models.Model):
@ -404,6 +410,7 @@ If you ever see output that looks like ``"hello
If you don't like the long ``ugettext_lazy`` name, you can just alias it as If you don't like the long ``ugettext_lazy`` name, you can just alias it as
``_`` (underscore), like so:: ``_`` (underscore), like so::
from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
class MyThing(models.Model): class MyThing(models.Model):
@ -429,6 +436,9 @@ definition. Therefore, you are authorized to pass a key name instead of an
integer as the ``number`` argument. Then ``number`` will be looked up in the integer as the ``number`` argument. Then ``number`` will be looked up in the
dictionary under that key during string interpolation. Here's example:: dictionary under that key during string interpolation. Here's example::
from django import forms
from django.utils.translation import ugettext_lazy
class MyForm(forms.Form): class MyForm(forms.Form):
error_message = ungettext_lazy("You only provided %(num)d argument", error_message = ungettext_lazy("You only provided %(num)d argument",
"You only provided %(num)d arguments", 'num') "You only provided %(num)d arguments", 'num')
@ -461,6 +471,7 @@ that concatenates its contents *and* converts them to strings only when the
result is included in a string. For example:: result is included in a string. For example::
from django.utils.translation import string_concat from django.utils.translation import string_concat
from django.utils.translation import ugettext_lazy
... ...
name = ugettext_lazy('John Lennon') name = ugettext_lazy('John Lennon')
instrument = ugettext_lazy('guitar') instrument = ugettext_lazy('guitar')
@ -1663,6 +1674,8 @@ preference available as ``request.LANGUAGE_CODE`` for each
:class:`~django.http.HttpRequest`. Feel free to read this value in your view :class:`~django.http.HttpRequest`. Feel free to read this value in your view
code. Here's a simple example:: code. Here's a simple example::
from django.http import HttpResponse
def hello_world(request, count): def hello_world(request, count):
if request.LANGUAGE_CODE == 'de-at': if request.LANGUAGE_CODE == 'de-at':
return HttpResponse("You prefer to read Austrian German.") return HttpResponse("You prefer to read Austrian German.")