| 814 | | There even is a ``ungettext`` interface and a string interpolation function:: |
|---|
| 815 | | |
|---|
| 816 | | d = { |
|---|
| 817 | | count: 10 |
|---|
| 818 | | }; |
|---|
| 819 | | s = interpolate(ungettext('this is %(count)s object', 'this are %(count)s objects', d.count), d); |
|---|
| 820 | | |
|---|
| 821 | | The ``interpolate`` function supports both positional interpolation and named |
|---|
| 822 | | interpolation. So the above could have been written as:: |
|---|
| 823 | | |
|---|
| 824 | | s = interpolate(ungettext('this is %s object', 'this are %s objects', 11), [11]); |
|---|
| 825 | | |
|---|
| 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). |
|---|
| | 814 | There is also an ``ngettext`` interface:: |
|---|
| | 815 | |
|---|
| | 816 | var object_cnt = 1 // or 0, or 2, or 3, ... |
|---|
| | 817 | s = ngettext('literal for the singular case', |
|---|
| | 818 | 'literal for the plural case', object_cnt); |
|---|
| | 819 | |
|---|
| | 820 | and even a string interpolation function:: |
|---|
| | 821 | |
|---|
| | 822 | function interpolate(fmt, obj, named); |
|---|
| | 823 | |
|---|
| | 824 | The interpolation syntax is borrowed from Python, so the ``interpolate`` |
|---|
| | 825 | function supports both positional and named interpolation: |
|---|
| | 826 | |
|---|
| | 827 | * Positional interpolation: ``obj`` contains a JavaScript Array object |
|---|
| | 828 | whose elements values are then sequentially interpolated in their |
|---|
| | 829 | corresponding ``fmt`` placeholders in the same order they appear. |
|---|
| | 830 | For example:: |
|---|
| | 831 | |
|---|
| | 832 | fmts = ngettext('There is %s object. Remaining: %s', |
|---|
| | 833 | 'There are %s objects. Remaining: %s', 11); |
|---|
| | 834 | s = interpolate(fmts, [11, 20]); |
|---|
| | 835 | // s is 'There are 11 objects. Remaining: 20' |
|---|
| | 836 | |
|---|
| | 837 | * Named interpolation: This mode is selected by passing the optional |
|---|
| | 838 | boolean ``named`` parameter as true. ``obj`` contains a JavaScript |
|---|
| | 839 | object or associative array. For example:: |
|---|
| | 840 | |
|---|
| | 841 | d = { |
|---|
| | 842 | count: 10 |
|---|
| | 843 | total: 50 |
|---|
| | 844 | }; |
|---|
| | 845 | |
|---|
| | 846 | fmts = ngettext('Total: %(total)s, there is %(count)s object', |
|---|
| | 847 | 'there are %(count)s of a total of %(total)s objects', d.count); |
|---|
| | 848 | s = interpolate(fmts, d, true); |
|---|
| | 849 | |
|---|
| | 850 | You shouldn't go over the top with string interpolation, though: this is still |
|---|
| | 851 | JavaScript, so the code has to make repeated regular-expression substitutions. |
|---|
| | 852 | This isn't as fast as string interpolation in Python, so keep it to those |
|---|
| | 853 | cases where you really need it (for example, in conjunction with ``ngettext`` |
|---|
| | 854 | to produce proper pluralizations). |
|---|