#46683 closed defect (bug) (fixed)
Site Health: audit the translation functions
Reported by: | afercia | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.2 | Priority: | normal |
Severity: | normal | Version: | 5.2 |
Component: | Site Health | Keywords: | site-health has-patch |
Focuses: | coding-standards | Cc: |
Description (last modified by )
Splitting this out from https://core.trac.wordpress.org/ticket/46573#comment:46
This kind of strings:
<span class="issue-count">0</span> <?php _e( 'Critical issues' ); ?>
(note: the count gets updated via JavaScript) are not translatable in languages that have multiple plural forms. This needs to be changed and meet the best practices used in core for proper localization. There are a few cases like this that should be addressed.
To my knowledge, the best option is to use a placeholder that gets then replaced via JavaScript: I see in other parts of the Site Health code this is implemented correctly.
/Cc @SergeyBiryukov
Also, I'd argue the following usages of context:
_ex( 'Site Health', 'Menu, Section and Page Title' )
is not the best way to use the context parameter: the context shouldn't be an "explanation". Instead, it should be a very short reference to what the strings refer to, meant to avoid collisions with similar translatable text. Also, in this specific case, not sure what is the value added by specifying "Menu, Section and Page Title".
Attachments (4)
Change History (29)
#8
follow-up:
↓ 10
@
6 years ago
I think I caught everything from comment:2, except for this item:
wp.i18n
can be used to translate strings in JavaScript
The main issue from the ticket description with strings like this is also still relevant:
<span class="issue-count">0</span> <?php _e( 'Critical issues' ); ?>
#10
in reply to:
↑ 8
@
6 years ago
Replying to SergeyBiryukov:
The main issue from the ticket description with strings like this is also still relevant:
<span class="issue-count">0</span> <?php _e( 'Critical issues' ); ?>
To fix this you can use _n()
and sprintf()
from the wp.i18n
package like so:
var _n = wp.i18n._n; var sprintf = wp.i18n.sprintf; var headline = sprintf( _n( '%s Critical Issues', '%s Critical Issues', count ), '<span class="issue-count">' + count + '</span>' );
The result should replace the inner HTML of the h3
element.
Any string that's added via SiteHealth.string
should probably be switched to using wp.i18n
.
This ticket was mentioned in Slack in #core by clorith. View the logs.
6 years ago
#12
@
6 years ago
- Keywords has-patch added; needs-patch removed
The result should replace the inner HTML of the h3 element.
Implemented. I used the > h3
selector to get that immediate h3
element for safety. That could have a class added instead.
Any string that's added via SiteHealth.string should probably be switched to using wp.i18n.
I replaced SiteHealth.string.site_health_complete_screen_reader
and site_info_copied
with wp.i18n
. I couldn't find any usages of the other localized strings.
There were also two "Copied!" strings that weren't translated.
I also modified the original PHP printed headlines to use _n
as well so that languages that have 0 as singular could do that. The 0
is constant though, so maybe it'd be better to have a translator comment like This is referring to 0 issues
or similar?
#13
@
6 years ago
Fixes a merge conflict and replaces the please_wait
and site_health_complete
strings.
This ticket was mentioned in Slack in #core by desrosj. View the logs.
6 years ago
#18
@
6 years ago
Coding standards: Variables declared with var
still need to be at the top of a function block. In this specific case: https://core.trac.wordpress.org/browser/trunk/src/js/_enqueues/admin/site-health.js?rev=45178&marks=149-150#L138 I'm not sure why a variable is used in the first place. The string can be directly passed to sprintf()
.
#20
@
6 years ago
I used the
> h3
selector to get that immediate h3 element for safety. That could have a class added instead.
Let's do it :) JS selectors shouldn't depend on a specific markup that may change at any time in the future.
#22
@
6 years ago
PHPCS isn't happy because of missing translators comments:
FILE: src/wp-admin/site-health.php ------------------------------------------------------------------------------------------------------------------------- FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES ------------------------------------------------------------------------------------------------------------------------- 94 | WARNING | A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment ... 102 | WARNING | A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment ... 118 | WARNING | A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment ...
wp.18n
can be used to translate strings in JavaScript%1$s:
should be just the number1
_x( 'https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions', 'The address to describe PHP modules and their use.' )
should be a translators comment__( 'the team handbook' )
should not be translated alone, instead add<a href="%s">the team handbook</a>
back to the string.<code>WP_DEBUG_LOG</code>