WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 21 months ago

#38147 new enhancement

Add sprintf.js library to WordPress

Reported by: swissspidy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: External Libraries Keywords: has-patch
Focuses: javascript Cc:

Description

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 21 months ago.

Download all attachments as: .zip

Change History (6)

@swissspidy
21 months ago

#1 @swissspidy
21 months 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
21 months ago

  • Keywords reporter-feedback added

What are some example use cases of this?

#3 @swissspidy
21 months 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
21 months ago

  • Keywords reporter-feedback removed

#5 @swissspidy
21 months 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

Note: See TracTickets for help on using tickets.