I18N: Use `wp.i18n` for translatable strings in `wp-admin/js/tags-box.js` and `wp-admin/js/tags-suggest.js`.

This removes the usage of `wp_localize_script()` for passing translations to the script and instead adds the translatable strings in the script directly through the use of `wp.i18n` and its utilities.

Props swissspidy, ocean90.
See #20491.
Fixes #50579.
Built from https://develop.svn.wordpress.org/trunk@48350


git-svn-id: http://core.svn.wordpress.org/trunk@48119 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2020-07-06 21:00:03 +00:00
parent 04921ffd45
commit 3d95a45675
6 changed files with 12 additions and 21 deletions

View File

@ -6,7 +6,7 @@
/* global ajaxurl, tagBox, array_unique_noempty */
( function( $ ) {
var tagDelimiter = ( window.tagsSuggestL10n && window.tagsSuggestL10n.tagDelimiter ) || ',';
var tagDelimiter = wp.i18n._x( ',', 'tag delimiter' ) || ',';
/**
* Filters unique items and returns a new array.
@ -164,7 +164,7 @@
*/
xbutton = $( '<button type="button" id="' + id + '-check-num-' + key + '" class="ntdelbutton">' +
'<span class="remove-tag-icon" aria-hidden="true"></span>' +
'<span class="screen-reader-text">' + window.tagsSuggestL10n.removeTerm + ' ' + listItem.html() + '</span>' +
'<span class="screen-reader-text">' + wp.i18n.__( 'Remove term:' ) + ' ' + listItem.html() + '</span>' +
'</button>' );
/**
@ -334,11 +334,11 @@
switch ( this.userAction ) {
case 'remove':
message = window.tagsSuggestL10n.termRemoved;
message = wp.i18n.__( 'Term removed.' );
break;
case 'add':
message = window.tagsSuggestL10n.termAdded;
message = wp.i18n.__( 'Term added.' );
break;
default:

View File

@ -1,2 +1,2 @@
/*! This file is auto-generated */
!function(r){var u=window.tagsSuggestL10n&&window.tagsSuggestL10n.tagDelimiter||",";window.array_unique_noempty=function(t){var a=[];return r.each(t,function(t,e){(e=r.trim(e))&&-1===r.inArray(e,a)&&a.push(e)}),a},window.tagBox={clean:function(t){return","!==u&&(t=t.replace(new RegExp(u,"g"),",")),t=t.replace(/\s*,\s*/g,",").replace(/,+/g,",").replace(/[,\s]+$/,"").replace(/^[,\s]+/,""),","!==u&&(t=t.replace(/,/g,u)),t},parseTags:function(t){var e=t.id.split("-check-num-")[1],a=r(t).closest(".tagsdiv"),s=a.find(".the-tags"),i=s.val().split(u),n=[];return delete i[e],r.each(i,function(t,e){(e=r.trim(e))&&n.push(e)}),s.val(this.clean(n.join(u))),this.quickClicks(a),!1},quickClicks:function(t){var e,i,a=r(".the-tags",t),n=r(".tagchecklist",t),c=r(t).attr("id");a.length&&(i=a.prop("disabled"),e=a.val().split(u),n.empty(),r.each(e,function(t,e){var a,s;(e=r.trim(e))&&(a=r("<li />").text(e),i||((s=r('<button type="button" id="'+c+"-check-num-"+t+'" class="ntdelbutton"><span class="remove-tag-icon" aria-hidden="true"></span><span class="screen-reader-text">'+window.tagsSuggestL10n.removeTerm+" "+a.html()+"</span></button>")).on("click keypress",function(t){"click"!==t.type&&13!==t.keyCode&&32!==t.keyCode||(13!==t.keyCode&&32!==t.keyCode||r(this).closest(".tagsdiv").find("input.newtag").focus(),tagBox.userAction="remove",tagBox.parseTags(this))}),a.prepend("&nbsp;").prepend(s)),n.append(a))}),tagBox.screenReadersMessage())},flushTags:function(t,e,a){var s,i,n,c=r(".the-tags",t),o=r("input.newtag",t);return void 0===(n=(e=e||!1)?r(e).text():o.val())||""===n||(i=(s=c.val())?s+u+n:n,i=this.clean(i),i=array_unique_noempty(i.split(u)).join(u),c.val(i),this.quickClicks(t),e||o.val(""),void 0===a&&o.focus()),!1},get:function(a){var s=a.substr(a.indexOf("-")+1);r.post(ajaxurl,{action:"get-tagcloud",tax:s},function(t,e){0!==t&&"success"==e&&(t=r('<div id="tagcloud-'+s+'" class="the-tagcloud">'+t+"</div>"),r("a",t).click(function(){return tagBox.userAction="add",tagBox.flushTags(r("#"+s),this),!1}),r("#"+a).after(t))})},userAction:"",screenReadersMessage:function(){var t;switch(this.userAction){case"remove":t=window.tagsSuggestL10n.termRemoved;break;case"add":t=window.tagsSuggestL10n.termAdded;break;default:return}window.wp.a11y.speak(t,"assertive")},init:function(){var t=r("div.ajaxtag");r(".tagsdiv").each(function(){tagBox.quickClicks(this)}),r(".tagadd",t).click(function(){tagBox.userAction="add",tagBox.flushTags(r(this).closest(".tagsdiv"))}),r("input.newtag",t).keypress(function(t){13==t.which&&(tagBox.userAction="add",tagBox.flushTags(r(this).closest(".tagsdiv")),t.preventDefault(),t.stopPropagation())}).each(function(t,e){r(e).wpTagsSuggest()}),r("#post").submit(function(){r("div.tagsdiv").each(function(){tagBox.flushTags(this,!1,1)})}),r(".tagcloud-link").click(function(){tagBox.get(r(this).attr("id")),r(this).attr("aria-expanded","true").unbind().click(function(){r(this).attr("aria-expanded","false"===r(this).attr("aria-expanded")?"true":"false").siblings(".the-tagcloud").toggle()})})}}}(jQuery);
!function(r){var u=wp.i18n._x(",","tag delimiter")||",";window.array_unique_noempty=function(t){var a=[];return r.each(t,function(t,e){(e=r.trim(e))&&-1===r.inArray(e,a)&&a.push(e)}),a},window.tagBox={clean:function(t){return","!==u&&(t=t.replace(new RegExp(u,"g"),",")),t=t.replace(/\s*,\s*/g,",").replace(/,+/g,",").replace(/[,\s]+$/,"").replace(/^[,\s]+/,""),","!==u&&(t=t.replace(/,/g,u)),t},parseTags:function(t){var e=t.id.split("-check-num-")[1],a=r(t).closest(".tagsdiv"),i=a.find(".the-tags"),n=i.val().split(u),s=[];return delete n[e],r.each(n,function(t,e){(e=r.trim(e))&&s.push(e)}),i.val(this.clean(s.join(u))),this.quickClicks(a),!1},quickClicks:function(t){var e,n,a=r(".the-tags",t),s=r(".tagchecklist",t),c=r(t).attr("id");a.length&&(n=a.prop("disabled"),e=a.val().split(u),s.empty(),r.each(e,function(t,e){var a,i;(e=r.trim(e))&&(a=r("<li />").text(e),n||((i=r('<button type="button" id="'+c+"-check-num-"+t+'" class="ntdelbutton"><span class="remove-tag-icon" aria-hidden="true"></span><span class="screen-reader-text">'+wp.i18n.__("Remove term:")+" "+a.html()+"</span></button>")).on("click keypress",function(t){"click"!==t.type&&13!==t.keyCode&&32!==t.keyCode||(13!==t.keyCode&&32!==t.keyCode||r(this).closest(".tagsdiv").find("input.newtag").focus(),tagBox.userAction="remove",tagBox.parseTags(this))}),a.prepend("&nbsp;").prepend(i)),s.append(a))}),tagBox.screenReadersMessage())},flushTags:function(t,e,a){var i,n,s,c=r(".the-tags",t),o=r("input.newtag",t);return void 0===(s=(e=e||!1)?r(e).text():o.val())||""===s||(n=(i=c.val())?i+u+s:s,n=this.clean(n),n=array_unique_noempty(n.split(u)).join(u),c.val(n),this.quickClicks(t),e||o.val(""),void 0===a&&o.focus()),!1},get:function(a){var i=a.substr(a.indexOf("-")+1);r.post(ajaxurl,{action:"get-tagcloud",tax:i},function(t,e){0!==t&&"success"==e&&(t=r('<div id="tagcloud-'+i+'" class="the-tagcloud">'+t+"</div>"),r("a",t).click(function(){return tagBox.userAction="add",tagBox.flushTags(r("#"+i),this),!1}),r("#"+a).after(t))})},userAction:"",screenReadersMessage:function(){var t;switch(this.userAction){case"remove":t=wp.i18n.__("Term removed.");break;case"add":t=wp.i18n.__("Term added.");break;default:return}window.wp.a11y.speak(t,"assertive")},init:function(){var t=r("div.ajaxtag");r(".tagsdiv").each(function(){tagBox.quickClicks(this)}),r(".tagadd",t).click(function(){tagBox.userAction="add",tagBox.flushTags(r(this).closest(".tagsdiv"))}),r("input.newtag",t).keypress(function(t){13==t.which&&(tagBox.userAction="add",tagBox.flushTags(r(this).closest(".tagsdiv")),t.preventDefault(),t.stopPropagation())}).each(function(t,e){r(e).wpTagsSuggest()}),r("#post").submit(function(){r("div.tagsdiv").each(function(){tagBox.flushTags(this,!1,1)})}),r(".tagcloud-link").click(function(){tagBox.get(r(this).attr("id")),r(this).attr("aria-expanded","true").unbind().click(function(){r(this).attr("aria-expanded","false"===r(this).attr("aria-expanded")?"true":"false").siblings(".the-tagcloud").toggle()})})}}}(jQuery);

View File

@ -4,12 +4,12 @@
* @output wp-admin/js/tags-suggest.js
*/
( function( $ ) {
if ( typeof window.tagsSuggestL10n === 'undefined' || typeof window.uiAutocompleteL10n === 'undefined' ) {
if ( typeof window.uiAutocompleteL10n === 'undefined' ) {
return;
}
var tempID = 0;
var separator = window.tagsSuggestL10n.tagDelimiter || ',';
var separator = wp.i18n._x( ',', 'tag delimiter' ) || ',';
function split( val ) {
return val.split( new RegExp( separator + '\\s*' ) );
@ -104,7 +104,7 @@
if ( $.ui.keyCode.TAB === event.keyCode ) {
// Audible confirmation message when a tag has been selected.
window.wp.a11y.speak( window.tagsSuggestL10n.termSelected, 'assertive' );
window.wp.a11y.speak( wp.i18n.__( 'Term selected.' ), 'assertive' );
event.preventDefault();
} else if ( $.ui.keyCode.ENTER === event.keyCode ) {
// If we're in the edit post Tags meta box, add the tag.

View File

@ -1,2 +1,2 @@
/*! This file is auto-generated */
!function(r){if(void 0!==window.tagsSuggestL10n&&void 0!==window.uiAutocompleteL10n){var s=0,a=window.tagsSuggestL10n.tagDelimiter||",";r.fn.wpTagsSuggest=function(t){var i,o,n=r(this),u=(t=t||{}).taxonomy||n.attr("data-wp-taxonomy")||"post_tag";return delete t.taxonomy,t=r.extend({source:function(t,a){var e;o!==t.term?(e=function(t){return l(t).pop()}(t.term),r.get(window.ajaxurl,{action:"ajax-tag-search",tax:u,q:e}).always(function(){n.removeClass("ui-autocomplete-loading")}).done(function(t){var e,o=[];if(t){for(e in t=t.split("\n")){var n=++s;o.push({id:n,name:t[e]})}a(i=o)}else a(o)}),o=t.term):a(i)},focus:function(t,e){n.attr("aria-activedescendant","wp-tags-autocomplete-"+e.item.id),t.preventDefault()},select:function(t,e){var o=l(n.val());return o.pop(),o.push(e.item.name,""),n.val(o.join(a+" ")),r.ui.keyCode.TAB===t.keyCode?(window.wp.a11y.speak(window.tagsSuggestL10n.termSelected,"assertive"),t.preventDefault()):r.ui.keyCode.ENTER===t.keyCode&&(window.tagBox&&(window.tagBox.userAction="add",window.tagBox.flushTags(r(this).closest(".tagsdiv"))),t.preventDefault(),t.stopPropagation()),!1},open:function(){n.attr("aria-expanded","true")},close:function(){n.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2",at:"left bottom",collision:"none"},messages:{noResults:window.uiAutocompleteL10n.noResults,results:function(t){return 1<t?window.uiAutocompleteL10n.manyResults.replace("%d",t):window.uiAutocompleteL10n.oneResult}}},t),n.on("keydown",function(){n.removeAttr("aria-activedescendant")}).autocomplete(t).autocomplete("instance")._renderItem=function(t,e){return r('<li role="option" id="wp-tags-autocomplete-'+e.id+'">').text(e.name).appendTo(t)},n.attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":n.autocomplete("widget").attr("id")}).on("focus",function(){l(n.val()).pop()&&n.autocomplete("search")}).autocomplete("widget").addClass("wp-tags-autocomplete").attr("role","listbox").removeAttr("tabindex").on("menufocus",function(t,e){e.item.attr("aria-selected","true")}).on("menublur",function(){r(this).find('[aria-selected="true"]').removeAttr("aria-selected")}),this}}function l(t){return t.split(new RegExp(a+"\\s*"))}}(jQuery);
!function(u){if(void 0!==window.uiAutocompleteL10n){var s=0,a=wp.i18n._x(",","tag delimiter")||",";u.fn.wpTagsSuggest=function(e){var i,o,n=u(this),r=(e=e||{}).taxonomy||n.attr("data-wp-taxonomy")||"post_tag";return delete e.taxonomy,e=u.extend({source:function(e,a){var t;o!==e.term?(t=function(e){return l(e).pop()}(e.term),u.get(window.ajaxurl,{action:"ajax-tag-search",tax:r,q:t}).always(function(){n.removeClass("ui-autocomplete-loading")}).done(function(e){var t,o=[];if(e){for(t in e=e.split("\n")){var n=++s;o.push({id:n,name:e[t]})}a(i=o)}else a(o)}),o=e.term):a(i)},focus:function(e,t){n.attr("aria-activedescendant","wp-tags-autocomplete-"+t.item.id),e.preventDefault()},select:function(e,t){var o=l(n.val());return o.pop(),o.push(t.item.name,""),n.val(o.join(a+" ")),u.ui.keyCode.TAB===e.keyCode?(window.wp.a11y.speak(wp.i18n.__("Term selected."),"assertive"),e.preventDefault()):u.ui.keyCode.ENTER===e.keyCode&&(window.tagBox&&(window.tagBox.userAction="add",window.tagBox.flushTags(u(this).closest(".tagsdiv"))),e.preventDefault(),e.stopPropagation()),!1},open:function(){n.attr("aria-expanded","true")},close:function(){n.attr("aria-expanded","false")},minLength:2,position:{my:"left top+2",at:"left bottom",collision:"none"},messages:{noResults:window.uiAutocompleteL10n.noResults,results:function(e){return 1<e?window.uiAutocompleteL10n.manyResults.replace("%d",e):window.uiAutocompleteL10n.oneResult}}},e),n.on("keydown",function(){n.removeAttr("aria-activedescendant")}).autocomplete(e).autocomplete("instance")._renderItem=function(e,t){return u('<li role="option" id="wp-tags-autocomplete-'+t.id+'">').text(t.name).appendTo(e)},n.attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":n.autocomplete("widget").attr("id")}).on("focus",function(){l(n.val()).pop()&&n.autocomplete("search")}).autocomplete("widget").addClass("wp-tags-autocomplete").attr("role","listbox").removeAttr("tabindex").on("menufocus",function(e,t){t.item.attr("aria-selected","true")}).on("menublur",function(){u(this).find('[aria-selected="true"]').removeAttr("aria-selected")}),this}}function l(e){return e.split(new RegExp(a+"\\s*"))}}(jQuery);

View File

@ -1212,19 +1212,10 @@ function wp_default_scripts( $scripts ) {
$scripts->set_translations( 'postbox' );
$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 );
did_action( 'init' ) && $scripts->localize(
'tags-suggest',
'tagsSuggestL10n',
array(
'tagDelimiter' => _x( ',', 'tag delimiter' ),
'removeTerm' => __( 'Remove term:' ),
'termSelected' => __( 'Term selected.' ),
'termAdded' => __( 'Term added.' ),
'termRemoved' => __( 'Term removed.' ),
)
);
$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->set_translations( 'post' );

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-48349';
$wp_version = '5.5-alpha-48350';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.