_n javascript variant?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

_n javascript variant?

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.

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
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: _n javascript variant?

Stephen Harris
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


_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: _n javascript variant?

J.D. Grimes
In reply to this post by TV productions
I’d take a look at https://core.trac.wordpress.org/ticket/20491

-J.D.

On Jul 16, 2014, at 11:32 AM, TV productions <[hidden email]> wrote:

> 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
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers