[wp-trac] [WordPress Trac] #38147: Add sprintf.js library to WordPress

WordPress Trac noreply at wordpress.org
Sat Sep 24 12:15:53 UTC 2016


#38147: Add sprintf.js library to WordPress
-----------------------------------------+------------------------------
 Reporter:  swissspidy                   |       Owner:
     Type:  enhancement                  |      Status:  new
 Priority:  normal                       |   Milestone:  Awaiting Review
Component:  External Libraries           |     Version:
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch reporter-feedback  |     Focuses:  javascript
-----------------------------------------+------------------------------

Comment (by swissspidy):

 For example, updates.js contains the following line:

 {{{#!js
 message  = wp.updates.l10n.updatingLabel.replace( '%s', $message.data(
 'name' ) );
 }}}

 #20491 allows us to improve this a bit:

 {{{#!js
 message  = wp.i18n__( 'Updating %s...' ).replace( '%s', $message.data(
 'name' ) );
 }}}

 However, this only really works well when there's a single placeholder to
 replace. It gets more complicated with additional placeholders and as soon
 as you want to deal with plurals in JavaScript (see #22229). Using
 `sprintf()`, which WordPress devs are much more familiar with, would
 greatly improve readability and flexibility of the code, especially when
 dealing with translations.

 [attachment:20491-applied.diff:ticket:20491] is a proof-of-concept that
 shows various places where `sprintf()` in JS could be used in core. For
 the above example, it would look like this:

 {{{#!js
 message  = sprintf( wp.i18n__( 'Updating %s...' ), $message.data( 'name' )
 );
 }}}

 With more and more plugins focusing on JavaScript-heavy admin interfaces,
 such utility functions become more important. By bundling this library
 with core, plugins do not have to enqueue it on their own — perhaps even
 multiple times — and core can benefit from it as well.

 It's worth noting that Jed.js, being suggested for inclusion in #20491,
 bundles a modified version of sprintf.js inline, but it's woefully out of
 date and not used in the current patch and thus not exposed to the global
 scope.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38147#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list