Ticket #6859: js-i18n-doc.2.diff
File js-i18n-doc.2.diff, 3.8 KB (added by , 17 years ago) |
---|
-
docs/i18n.txt
diff -r da74ed639346 docs/i18n.txt
a b take a string as their first argument an 338 338 take a string as their first argument and do something to that string. These 339 339 functions are used by template filters as well as directly in other code. 340 340 341 If you write your own similar functions and deal with translations, you'll 341 If you write your own similar functions and deal with translations, you'll 342 342 face the problem of what to do when the first argument is a lazy translation 343 343 object. You don't want to convert it to a string immediately, because you might 344 344 be using this function outside of a view (and hence the current thread's locale … … You can make the view dynamic by putting 789 789 You can make the view dynamic by putting the packages into the URL pattern:: 790 790 791 791 urlpatterns = patterns('', 792 (r'^jsi18n/(?P<packages>\S+?)/$ , 'django.views.i18n.javascript_catalog'),792 (r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'), 793 793 ) 794 794 795 795 With this, you specify the packages as a list of package names delimited by '+' … … interface to access it:: 811 811 812 812 document.write(gettext('this is to be translated')); 813 813 814 There even is a ``ungettext`` interface and a string interpolation function::814 There is also a ``ngettext`` interface:: 815 815 816 d = { 817 count: 10 818 }; 819 s = interpolate(ungettext('this is %(count)s object', 'this are %(count)s objects', d.count), d); 816 var object_cnt = 1 // or 0, or 2, or 3, ... 817 s = ngettext('literal for the singular case', 'literal for the plural case', object_cnt); 820 818 821 The ``interpolate`` function supports both positional interpolation and named 822 interpolation. So the above could have been written as:: 819 and even a string interpolation function:: 823 820 824 s = interpolate(ungettext('this is %s object', 'this are %s objects', 11), [11]);821 function interpolate(fmt, obj, named); 825 822 826 The interpolation syntax is borrowed from Python. You shouldn't go over the top 827 with string interpolation, though: this is still JavaScript, so the code will 828 have to do repeated regular-expression substitutions. This isn't as fast as 829 string interpolation in Python, so keep it to those cases where you really 830 need it (for example, in conjunction with ``ungettext`` to produce proper 831 pluralizations). 823 The interpolation syntax is borrowed from Python, so the ``interpolate`` function 824 supports both positional interpolation and named interpolation: 825 826 * Positional interpolation: *obj* contains a JavaScript Array object 827 whose elements values are then sequentially interpolated in their 828 corresponding *fmt* placeholders in the same order they appear. 829 For example:: 830 831 fmts = ngettext('There is %s object. Remaining: %s', 'There are %s objects. Remaining: %s', 11); 832 s = interpolate(fmts, [11, 20]); 833 // s is 'There are 11 objects. Remaining: 20' 834 835 * Named interpolation: This mode is selected by passing the optional 836 boolean *named* parameter as true. *obj* contains a JavaScript 837 object or associative array. For example:: 838 839 d = { 840 count: 10 841 total: 50 842 }; 843 fmts = ngettext('Total: %(total)s, there is %(count)s object', 844 'there are %(count)s of a total of %(total)s objects', d.count); 845 s = interpolate(fmts, d, true); 846 847 You shouldn't go over the top with string interpolation, though: this is still 848 JavaScript, so the code will have to do repeated regular-expression 849 substitutions. This isn't as fast as string interpolation in Python, so keep 850 it to those cases where you really need it (for example, in conjunction with 851 ``ngettext`` to produce proper pluralizations). 832 852 833 853 Creating JavaScript translation catalogs 834 854 ----------------------------------------