[wp-trac] [WordPress Trac] #37057: Creation of an esc_html functions for _n(), _nx(), _ex(), and number_format_i18n()
WordPress Trac
noreply at wordpress.org
Mon Sep 6 15:18:22 UTC 2021
#37057: Creation of an esc_html functions for _n(), _nx(), _ex(), and
number_format_i18n()
-------------------------+------------------------------
Reporter: zakkath | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: I18N | Version:
Severity: normal | Resolution:
Keywords: needs-patch | Focuses:
-------------------------+------------------------------
Comment (by gonssal):
Replying to [comment:16 SergeyBiryukov]:
> Replying to [comment:14 gonssal]:
> > Simplified use case for {{{_n()}}}. Imagine I have an array of user
names I want to list. I want the list to have a translatable heading
specifying what the names are about:
> >
> > {{{#!php
> > esc_html_n( 'User:', 'Users:', count( $users ), 'textdomain' );
> > }}}
>
> Just noting that this is not the correct usage of `_n()`, see a note in
the [https://developer.wordpress.org/plugins/internationalization/how-to-
internationalize-your-plugin/#basic-pluralization plugin i18n handbook]:
>
> > Note that some languages use the singular form for other numbers (e.g.
21, 31 and so on, much like ’21st’, ’31st’ in English). If you would like
to special case the singular, check for it specifically:
> > {{{
> > if ( 1 === $count ) {
> > printf( esc_html__( 'Last thing!', 'my-text-domain' ), $count );
> > } else {
> > printf( esc_html( _n( '%d thing.', '%d things.', $count, 'my-text-
domain' ) ), $count );
> > }
> > }}}
>
> `_n()` should be used for strings that include a number, like `%s user`,
`%s users`, but not for `User` vs. `Users`.
>
> Similar issues were fixed in WordPress core a few years ago, see these
changesets:
>
> * [31941]: Decouple strings where the singular and plural form are not
just the same string with different numbers, but essentially two different
strings.
> * [31951]: After [31941], use the decoupled strings from
`WP_Customize_Manager::register_controls()` on the Menus screen.
> * [32029]: After [31941], use the decoupled strings from `wp-
admin/network/themes.php` in `wp-admin/network/site-themes.php` as well.
> * [34521]: Plugins: Don't use `_n()` for singular/plural strings which
have no placeholder for a number.
> * [35611]: About: Don't use `_n_noop()` for singular/plural strings
which provide no placeholder for a number.
> and these comments for more context:
>
> * comment:5:ticket:28502
> * comment:23:ticket:28502
> * comment:37:ticket:28502
>
> So the correct code for the use case above would look like this:
> {{{
> if ( 1 === count( $users ) ) {
> esc_html_e( 'User:', 'textdomain' );
> } else {
> esc_html_e( 'Users:', 'textdomain' );
> }
> }}}
It was just a quick example for an use case. You can modify the docs
example like this:
{{{#!php
if ( 1 === $count ) {
printf( esc_html__( 'Last thing!', 'my-text-domain' ), $count );
} else {
printf( esc_html_n( '%d thing.', '%d things.', $count, 'my-text-
domain' ), $count );
}
}}}
One function call instead of 2, there's your use case.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/37057#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list