Discussion:
_n javascript variant?
TV productions
2014-07-16 15:32:23 UTC
Permalink
Hi there,

I have a custom post type with a custom edit post screen. On that
screen, I've build an editor in javascript for this post type.
The point is, I've a label with the text "1 photo" or "2 photos". This
label is initial set on the server side and the number of photos changes
on the client side with javascript.

So on the server side I have this code:

<?php
printf(_n('%d photo', '%d photos', count($photos), 'textdomain'),
count($photos));
?>

And later on I change it with javascript. I currently use
wp_localize_script with:

array ('photo' => __('photo', 'textdomain'),
'photos' => __('photos', 'textdomain')
)

And in javascript the next condition to choose one of the two:

if ($photos.length == 1) {
$label.text('1 ' + myL10n.photo);
} else {
$label.text($photos.lenght + ' ' + myL10n.photos);
}

This would output things like "0 photos", "1 photo" and "2 photos".
The point is that some languages don't have the same plural forms for 0
and 2 OR that they have more than 1 plural form.

So does anyone know a workaround to get a correct translation, also in
languages with more than 1 plural form?

I hope you have some suggestions guys!


Best Regards,
Ties
--
TV productions :: Web development and stuff
http://tv-productions.org
Stephen Harris
2014-07-16 15:51:40 UTC
Permalink
I had a similar problem recently when working in a Backbone.js-heavy
project. If the number of translatable strings is low, then you could
still use wp_localize_script() to load the plural translations as we all
as singular. Obviously this isn't entirely ideal, as you don't know a
priori how many different plural forms there are (though this list is
pretty extensive
http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html).

For a variety of reasons I went down a different route of generating a
.json version of the .po file, and then used that data client-side with
few simple functions to look-up translations. You essentially just use
wp_localize_script() with the appropriate .json data. I use a Grunt task
to generate the .json files, so I don't need to worry about things
falling out of sync. This still has some drawbacks, not least because my
implementation only handles one plural form (I plan to improve on this).
Additionally, it lives out of the scope of "language packs" which .org
hosted plug-ins can take advantage of. Nevertheless, details of this
approach can be found here:
http://stephenharris.info/grunt-wordpress-development-iv-another-task-for-internationalisation/.

Stephen
J.D. Grimes
2014-07-16 16:00:13 UTC
Permalink
I’d take a look at https://core.trac.wordpress.org/ticket/20491

-J.D.
Post by TV productions
Hi there,
I have a custom post type with a custom edit post screen. On that screen, I've build an editor in javascript for this post type.
The point is, I've a label with the text "1 photo" or "2 photos". This label is initial set on the server side and the number of photos changes on the client side with javascript.
<?php
printf(_n('%d photo', '%d photos', count($photos), 'textdomain'), count($photos));
?>
array ('photo' => __('photo', 'textdomain'),
'photos' => __('photos', 'textdomain')
)
if ($photos.length == 1) {
$label.text('1 ' + myL10n.photo);
} else {
$label.text($photos.lenght + ' ' + myL10n.photos);
}
This would output things like "0 photos", "1 photo" and "2 photos".
The point is that some languages don't have the same plural forms for 0 and 2 OR that they have more than 1 plural form.
So does anyone know a workaround to get a correct translation, also in languages with more than 1 plural form?
I hope you have some suggestions guys!
Best Regards,
Ties
--
TV productions :: Web development and stuff
http://tv-productions.org
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...