[wp-trac] [WordPress Trac] #22229: Plurals in JavaScript

WordPress Trac noreply at wordpress.org
Mon Oct 22 00:20:19 UTC 2012


#22229: Plurals in JavaScript
-------------------------+------------------
 Reporter:  nacin        |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  3.5
Component:  I18N         |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |
-------------------------+------------------

Comment (by nacin):

 In WordPress, we register plural strings in a POT file like so:

 `_n( '%d string', '%d strings' );`

 English has two forms: a singular form (n = 1) and a plural form for n !=
 1. Other languages have anywhere from one (like Japanese) to four forms
 (like Slovenian). I'm sure you are familiar with Romanian's three forms.

 Using `_n()` alone is not sufficient. You also have to pass the number of
 items, so the proper translation is chosen for the language to which you
 are translating. So, `_n( '%d string', '%d strings', $number_of_strings
 );`. And, you will get back a string with %d (or %s, if the number could
 be sufficiently large enough to possibly need comma separators with
 `number_format_i18n()`). This is why we then `sprintf()` the result. So:

 `printf( _n( '%d string', '%d strings', $number_of_strings ),
 $number_of_strings );`

 Or:

 `printf( _n( '%s string', '%s strings', $number_of_strings ),
 number_format_i18n( $number_of_strings ) );`

 For JavaScript, we obviously do not have access to core's i18n libraries.
 So we pass strings via the script-loader. For example, `postL10n.publish`
 is `__( 'Publish' );`. But in order to translate a plural, we need to know
 the count of items (''n''), which then gets passed to an expression to
 determine the plural form to be used based.

 What `wp_js_i18n_plural()` does is provide a function in JS that can be
 called with the number of items, because these numbers are going to be
 dynamic. It does this by using the same plural expression (which takes
 ''n'' and returns the plural to use) and an array of all of the plural
 forms.

 So rather than `exampleL10n.numItems` being '%d items', it can be this:

 {{{
 exampleL10n.numItems = function(n) {
         var i = (n != 1),
                 t = {"0":"%d item","1":"%d items"};
         if (typeof i === 'boolean') i = i ? 1 : 0;
         return i > 1 ? t[1] : t[i];
 }
 }}}

 And then instead of invoking `exampleL10n.numItems`, you invoke
 `exampleL10n.numItems(5)`, and get back `%d items'. Or, if called with
 `(1)`, you'd get back '%d item'.

 The patch needs some work. One, we need to decide whether the JS function
 should do the sprintf automatically (my example does not; the patch does).
 Two, some of this logic needs to make its way back into the pomo classes,
 as right now it is dependent on Gettext_Translations to function.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22229#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list