From de1ed9c57b1d2c00abaa0e473bec6979595489c3 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 18 Feb 2024 22:14:04 +0000 Subject: [PATCH] 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 --- src/js/_enqueues/admin/common.js | 15 ++++++++++++ src/js/_enqueues/admin/tags-suggest.js | 23 +++++++++++-------- .../vendor/tinymce/plugins/wplink/plugin.js | 21 ++++++++++------- src/wp-includes/script-loader.php | 19 +++------------ 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js index 3de9447879..da7a7a3b07 100644 --- a/src/js/_enqueues/admin/common.js +++ b/src/js/_enqueues/admin/common.js @@ -353,6 +353,21 @@ window.setPostThumbnailL10n = window.setPostThumbnailL10n || { 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. * diff --git a/src/js/_enqueues/admin/tags-suggest.js b/src/js/_enqueues/admin/tags-suggest.js index f93396a77e..d439f06ef0 100644 --- a/src/js/_enqueues/admin/tags-suggest.js +++ b/src/js/_enqueues/admin/tags-suggest.js @@ -4,12 +4,11 @@ * @output wp-admin/js/tags-suggest.js */ ( function( $ ) { - if ( typeof window.uiAutocompleteL10n === 'undefined' ) { - return; - } - var tempID = 0; var separator = wp.i18n._x( ',', 'tag delimiter' ) || ','; + var __ = wp.i18n.__, + _n = wp.i18n._n, + sprintf = wp.i18n.sprintf; function split( val ) { return val.split( new RegExp( separator + '\\s*' ) ); @@ -139,13 +138,17 @@ collision: 'none' }, messages: { - noResults: window.uiAutocompleteL10n.noResults, + noResults: __( 'No results found.' ), results: function( number ) { - if ( number > 1 ) { - return window.uiAutocompleteL10n.manyResults.replace( '%d', number ); - } - - return window.uiAutocompleteL10n.oneResult; + return sprintf( + /* translators: %d: Number of search results found. */ + _n( + '%d result found. Use up and down arrow keys to navigate.', + '%d results found. Use up and down arrow keys to navigate.', + number + ), + number + ); } } }, options ); diff --git a/src/js/_enqueues/vendor/tinymce/plugins/wplink/plugin.js b/src/js/_enqueues/vendor/tinymce/plugins/wplink/plugin.js index 29f98a128f..c3433186a9 100644 --- a/src/js/_enqueues/vendor/tinymce/plugins/wplink/plugin.js +++ b/src/js/_enqueues/vendor/tinymce/plugins/wplink/plugin.js @@ -98,6 +98,9 @@ var urlRegex2 = /^https?:\/\/[^\/]+\.[^\/]+($|\/)/i; var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {}; var hasLinkError = false; + var __ = window.wp.i18n.__; + var _n = window.wp.i18n._n; + var sprintf = window.wp.i18n.sprintf; function getSelectedLink() { var href, html, @@ -457,15 +460,17 @@ my: 'left top+2' }, messages: { - noResults: ( typeof window.uiAutocompleteL10n !== 'undefined' ) ? window.uiAutocompleteL10n.noResults : '', + noResults: __( 'No results found.' ) , results: function( number ) { - if ( typeof window.uiAutocompleteL10n !== 'undefined' ) { - if ( number > 1 ) { - return window.uiAutocompleteL10n.manyResults.replace( '%d', number ); - } - - return window.uiAutocompleteL10n.oneResult; - } + return sprintf( + /* translators: %d: Number of search results found. */ + _n( + '%d result found. Use up and down arrow keys to navigate.', + '%d results found. Use up and down arrow keys to navigate.', + number + ), + number + ); } } } ).autocomplete( 'instance' )._renderItem = function( ul, item ) { diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index f41da6eff3..7fa4554d50 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -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-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. $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( '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( 'wplink', '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->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->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 );