Script Loader: Switch to JavaScript translations.

Update JavaScript files for tag suggestions and the TinyMCE link plugin to use client side translations. This allows for `_n()` to be used for strings requiring singular and plural versions in which the correct form is only known client side.

Props audrasjb, chaion07, costdev, hellofromtonya, johnbillion, marybaum, nicolefurlan, oglekler, rebasaurus, rsiddharth, sergeybiryukov, shaampk1, shahariaazam, swissspidy, tobifjellner.
Fixes #48244.



git-svn-id: https://develop.svn.wordpress.org/trunk@57654 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2024-02-18 22:14:04 +00:00
parent a9ba7fde79
commit de1ed9c57b
4 changed files with 44 additions and 34 deletions

View File

@ -353,6 +353,21 @@ window.setPostThumbnailL10n = window.setPostThumbnailL10n || {
window.setPostThumbnailL10n = deprecateL10nObject( 'setPostThumbnailL10n', window.setPostThumbnailL10n, '5.5.0' ); window.setPostThumbnailL10n = deprecateL10nObject( 'setPostThumbnailL10n', window.setPostThumbnailL10n, '5.5.0' );
/**
* Removed in 6.5.0, needed for back-compatibility.
*
* @since 4.5.0
* @deprecated 6.5.0
*/
window.uiAutocompleteL10n = window.uiAutocompleteL10n || {
noResults: '',
oneResult: '',
manyResults: '',
itemSelected: ''
};
window.uiAutocompleteL10n = deprecateL10nObject( 'uiAutocompleteL10n', window.uiAutocompleteL10n, '6.5.0' );
/** /**
* Removed in 3.3.0, needed for back-compatibility. * Removed in 3.3.0, needed for back-compatibility.
* *

View File

@ -4,12 +4,11 @@
* @output wp-admin/js/tags-suggest.js * @output wp-admin/js/tags-suggest.js
*/ */
( function( $ ) { ( function( $ ) {
if ( typeof window.uiAutocompleteL10n === 'undefined' ) {
return;
}
var tempID = 0; var tempID = 0;
var separator = wp.i18n._x( ',', 'tag delimiter' ) || ','; var separator = wp.i18n._x( ',', 'tag delimiter' ) || ',';
var __ = wp.i18n.__,
_n = wp.i18n._n,
sprintf = wp.i18n.sprintf;
function split( val ) { function split( val ) {
return val.split( new RegExp( separator + '\\s*' ) ); return val.split( new RegExp( separator + '\\s*' ) );
@ -139,13 +138,17 @@
collision: 'none' collision: 'none'
}, },
messages: { messages: {
noResults: window.uiAutocompleteL10n.noResults, noResults: __( 'No results found.' ),
results: function( number ) { results: function( number ) {
if ( number > 1 ) { return sprintf(
return window.uiAutocompleteL10n.manyResults.replace( '%d', number ); /* translators: %d: Number of search results found. */
} _n(
'%d result found. Use up and down arrow keys to navigate.',
return window.uiAutocompleteL10n.oneResult; '%d results found. Use up and down arrow keys to navigate.',
number
),
number
);
} }
} }
}, options ); }, options );

View File

@ -98,6 +98,9 @@
var urlRegex2 = /^https?:\/\/[^\/]+\.[^\/]+($|\/)/i; var urlRegex2 = /^https?:\/\/[^\/]+\.[^\/]+($|\/)/i;
var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {}; var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {};
var hasLinkError = false; var hasLinkError = false;
var __ = window.wp.i18n.__;
var _n = window.wp.i18n._n;
var sprintf = window.wp.i18n.sprintf;
function getSelectedLink() { function getSelectedLink() {
var href, html, var href, html,
@ -457,15 +460,17 @@
my: 'left top+2' my: 'left top+2'
}, },
messages: { messages: {
noResults: ( typeof window.uiAutocompleteL10n !== 'undefined' ) ? window.uiAutocompleteL10n.noResults : '', noResults: __( 'No results found.' ) ,
results: function( number ) { results: function( number ) {
if ( typeof window.uiAutocompleteL10n !== 'undefined' ) { return sprintf(
if ( number > 1 ) { /* translators: %d: Number of search results found. */
return window.uiAutocompleteL10n.manyResults.replace( '%d', number ); _n(
} '%d result found. Use up and down arrow keys to navigate.',
'%d results found. Use up and down arrow keys to navigate.',
return window.uiAutocompleteL10n.oneResult; number
} ),
number
);
} }
} }
} ).autocomplete( 'instance' )._renderItem = function( ul, item ) { } ).autocomplete( 'instance' )._renderItem = function( ul, item ) {

View File

@ -932,20 +932,6 @@ function wp_default_scripts( $scripts ) {
$scripts->add( 'jquery-ui-position', false, array( 'jquery-ui-core' ), '1.13.2', 1 ); $scripts->add( 'jquery-ui-position', false, array( 'jquery-ui-core' ), '1.13.2', 1 );
$scripts->add( 'jquery-ui-widget', false, array( 'jquery-ui-core' ), '1.13.2', 1 ); $scripts->add( 'jquery-ui-widget', false, array( 'jquery-ui-core' ), '1.13.2', 1 );
// Strings for 'jquery-ui-autocomplete' live region messages.
did_action( 'init' ) && $scripts->localize(
'jquery-ui-autocomplete',
'uiAutocompleteL10n',
array(
'noResults' => __( 'No results found.' ),
/* translators: Number of results found when using jQuery UI Autocomplete. */
'oneResult' => __( '1 result found. Use up and down arrow keys to navigate.' ),
/* translators: %d: Number of results found when using jQuery UI Autocomplete. */
'manyResults' => __( '%d results found. Use up and down arrow keys to navigate.' ),
'itemSelected' => __( 'Item selected.' ),
)
);
// Deprecated, not used in core, most functionality is included in jQuery 1.3. // Deprecated, not used in core, most functionality is included in jQuery 1.3.
$scripts->add( 'jquery-form', "/wp-includes/js/jquery/jquery.form$suffix.js", array( 'jquery' ), '4.3.0', 1 ); $scripts->add( 'jquery-form', "/wp-includes/js/jquery/jquery.form$suffix.js", array( 'jquery' ), '4.3.0', 1 );
@ -1247,7 +1233,8 @@ function wp_default_scripts( $scripts ) {
$scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", array( 'hoverintent-js' ), false, 1 ); $scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", array( 'hoverintent-js' ), false, 1 );
$scripts->add( 'wplink', "/wp-includes/js/wplink$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 ); $scripts->add( 'wplink', "/wp-includes/js/wplink$suffix.js", array( 'common', 'jquery', 'wp-a11y', 'wp-i18n' ), false, 1 );
$scripts->set_translations( 'wplink' );
did_action( 'init' ) && $scripts->localize( did_action( 'init' ) && $scripts->localize(
'wplink', 'wplink',
'wpLinkL10n', 'wpLinkL10n',
@ -1425,7 +1412,7 @@ function wp_default_scripts( $scripts ) {
$scripts->add( 'tags-box', "/wp-admin/js/tags-box$suffix.js", array( 'jquery', 'tags-suggest' ), false, 1 ); $scripts->add( 'tags-box', "/wp-admin/js/tags-box$suffix.js", array( 'jquery', 'tags-suggest' ), false, 1 );
$scripts->set_translations( 'tags-box' ); $scripts->set_translations( 'tags-box' );
$scripts->add( 'tags-suggest', "/wp-admin/js/tags-suggest$suffix.js", array( 'jquery-ui-autocomplete', 'wp-a11y' ), false, 1 ); $scripts->add( 'tags-suggest', "/wp-admin/js/tags-suggest$suffix.js", array( 'common', 'jquery-ui-autocomplete', 'wp-a11y', 'wp-i18n' ), false, 1 );
$scripts->set_translations( 'tags-suggest' ); $scripts->set_translations( 'tags-suggest' );
$scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count', 'wp-a11y', 'wp-sanitize', 'clipboard' ), false, 1 ); $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count', 'wp-a11y', 'wp-sanitize', 'clipboard' ), false, 1 );