Fixed #6859 -- Greatly cleaned up the section on i18n pluralization in
Javascript. With new example code and everything! Thanks, Ramiro Morales. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7357 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
eb6a6c4619
commit
a9a70bfe55
|
@ -789,7 +789,7 @@ JavaScript that uses strings from different applications.
|
||||||
You can make the view dynamic by putting the packages into the URL pattern::
|
You can make the view dynamic by putting the packages into the URL pattern::
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^jsi18n/(?P<packages>\S+?)/$, 'django.views.i18n.javascript_catalog'),
|
(r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
|
||||||
)
|
)
|
||||||
|
|
||||||
With this, you specify the packages as a list of package names delimited by '+'
|
With this, you specify the packages as a list of package names delimited by '+'
|
||||||
|
@ -811,24 +811,47 @@ interface to access it::
|
||||||
|
|
||||||
document.write(gettext('this is to be translated'));
|
document.write(gettext('this is to be translated'));
|
||||||
|
|
||||||
There even is a ``ungettext`` interface and a string interpolation function::
|
There is also an ``ngettext`` interface::
|
||||||
|
|
||||||
|
var object_cnt = 1 // or 0, or 2, or 3, ...
|
||||||
|
s = ngettext('literal for the singular case',
|
||||||
|
'literal for the plural case', object_cnt);
|
||||||
|
|
||||||
|
and even a string interpolation function::
|
||||||
|
|
||||||
|
function interpolate(fmt, obj, named);
|
||||||
|
|
||||||
|
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
|
||||||
|
whose elements values are then sequentially interpolated in their
|
||||||
|
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]);
|
||||||
|
// s is 'There are 11 objects. Remaining: 20'
|
||||||
|
|
||||||
|
* 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 = {
|
d = {
|
||||||
count: 10
|
count: 10
|
||||||
|
total: 50
|
||||||
};
|
};
|
||||||
s = interpolate(ungettext('this is %(count)s object', 'this are %(count)s objects', d.count), d);
|
|
||||||
|
|
||||||
The ``interpolate`` function supports both positional interpolation and named
|
fmts = ngettext('Total: %(total)s, there is %(count)s object',
|
||||||
interpolation. So the above could have been written as::
|
'there are %(count)s of a total of %(total)s objects', d.count);
|
||||||
|
s = interpolate(fmts, d, true);
|
||||||
|
|
||||||
s = interpolate(ungettext('this is %s object', 'this are %s objects', 11), [11]);
|
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.
|
||||||
The interpolation syntax is borrowed from Python. You shouldn't go over the top
|
This isn't as fast as string interpolation in Python, so keep it to those
|
||||||
with string interpolation, though: this is still JavaScript, so the code will
|
cases where you really need it (for example, in conjunction with ``ngettext``
|
||||||
have to do repeated regular-expression substitutions. This isn't as fast as
|
to produce proper pluralizations).
|
||||||
string interpolation in Python, so keep it to those cases where you really
|
|
||||||
need it (for example, in conjunction with ``ungettext`` to produce proper
|
|
||||||
pluralizations).
|
|
||||||
|
|
||||||
Creating JavaScript translation catalogs
|
Creating JavaScript translation catalogs
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue