Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#9673 closed enhancement (wontfix)

sprintf() shortcuts

Reported by: wnorris Owned by: nbachiyski
Milestone: Priority: normal
Severity: normal Version: 2.8
Component: I18N Keywords: 2nd-opinion
Focuses: Cc:


After seeing #9650, I figured it would equally make sense to add shortcut methods for the very often used case of formatted, localized strings:

sprintf( __('Author is %s'), $author )

I propose two new l10n functions: _f() and _ef() which accept a formatted string and a variable number of arguments, which are passed through __() and sprintf() as in the example above.

I've not yet attached a patch for this, because I'm not sure what to do about the domain. One possibility is to require that the domain be passed in, so the above example would be called as:

_f( 'Author is %s', 'default', $author)

I'm certainly not crazy about that approach, since it makes all the calls in WP core unnecessarily verbose. Alternately, you could take the variable arguments as an array, changing the function signature to be:

function _f( $string, $args, $domain = 'default')

This would result in the above example being called as:

_f( 'Author is %s', array($author) )

or if you want to specify the domain:

_f( 'Author is %s', array($author), 'my-domain' )

Would love to hear others thoughts on this. Perhaps this has even been discussed before?

Change History (6)

#1 @wnorris
9 years ago

just thinking out loud a bit... perhaps allow the first argument to be a string or an array of two strings (the string itself, and the domain). Then the simple use case would be:

_f( 'Author is %s', $author)

and the slightly more complex would be

_f( array('Author is %s', 'my-domain'), $author)

Hmm, but then the second example here is inconsistent with the other _*() functions, and is almost identical to simply calling

sprintf( __('Author is %s', 'my-domain'), $author)

#2 @filosofo
9 years ago

Why not make all these l10n functions into comparable object methods, and then set the domain as an object property? Then you could do something like the following:

$my_l10n = new WP_L10n('my-domain');
$my_l10n->_f('Author is %s', array($author))

#3 @Denis-de-Bernardy
9 years ago

  • Keywords 2nd-opinion added; i18n removed
  • Milestone changed from Unassigned to Future Release

if any _f() creeps in, it should probably be like this:

_('foo %s', $bar, 'domain');

#4 @Denis-de-Bernardy
9 years ago

but i'm really itching to close as wontfix, due to the fact that it'll introduce unneeded overhead.

#5 @DD32
9 years ago

i'm -1 to this idea personally, I like the idea of printf/spritf being there, It allows non-wordpress-based php developers to know that yes, that is just a straight sprintf function there.. and its being translated too (By the __())

#6 @Denis-de-Bernardy
9 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

let's close it, then

Note: See TracTickets for help on using tickets.