Make WordPress Core

Opened 5 years ago

Closed 3 years ago

#38147 closed enhancement (worksforme)

Add sprintf.js library to WordPress

Reported by: swissspidy Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: External Libraries Keywords: has-patch needs-refresh reporter-feedback
Focuses: javascript Cc:


Working on #20491 revealed a need for a sprintf() function in JavaScript.

Although ES6 supports some basic string formatting, it's not something core could use in its various JS files. Thus, I suggest adding the very popular sprintf.js library to WordPress.

The minified script is only 3.7 KB in size, the source file is 9.4 KB large. Adding it to core with the sprintf handle would not only benefit core but also various plugins and themes in need of such a functionality, especially with translations.

Attachments (1)

38147.diff (4.6 KB) - added by swissspidy 5 years ago.

Download all attachments as: .zip

Change History (8)

5 years ago

#1 @swissspidy
5 years ago

  • Keywords has-patch added; needs-patch removed
  • Type changed from defect (bug) to enhancement

38147.diff registers the minified version of sprintf.js

#2 @johnbillion
5 years ago

  • Keywords reporter-feedback added

What are some example use cases of this?

#3 @swissspidy
5 years ago

For example, updates.js contains the following line:

message  = wp.updates.l10n.updatingLabel.replace( '%s', $message.data( 'name' ) );

#20491 allows us to improve this a bit:

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.

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:

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.

#4 @swissspidy
5 years ago

  • Keywords reporter-feedback removed

#5 @swissspidy
5 years ago

38112.diff:ticket:38112 is another good example why we need a solution like that. For translators the sprintf arguments are easy to understand

#6 @desrosj
3 years ago

  • Keywords needs-refresh reporter-feedback added

@swissspidy with the recent I18N related JS work, is this still helpful? If so, the patch should be refreshed to include the latest version of sprintf.js, which is currently 1.1.2.

It is also an NPM package. With the new build process, it would be nice to include this as a dependency instead for easier updating.

#7 @swissspidy
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

@desrosj Oh wow, I completely forgot about this one :-)

sprintf() is now available via the @wordpress/i18n package, so this one can be considered fixed.

Note: See TracTickets for help on using tickets.