WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 6 weeks ago

#49484 reviewing feature request

Add option 'OR' to wp_sprintf_l()

Reported by: pedromendonca Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: Formatting Keywords: has-patch
Focuses: Cc:

Description

The result of a this function is filtered by wp_sprintf_l() with ' and ' type delimiters.

Would it be awesome to include a third optional argument, a delimiter type ' or ', keeping the default ' and ' for backward compatibility.

Current use:
wp_sprintf( '%s: %l', __( 'Some cool numbers' ), array( 1, 5, 10, 15 ) );

Output:
Some cool numbers: 1, 5, 10, and 15

Example of use:
wp_sprintf( '%s: %l', __( 'Some cool numbers' ), array( 1, 5, 10, 15 ), 'OR' );

Optional output:
Some cool numbers: 1, 5, 10, or 15

Attachments (3)

49484.diff (3.9 KB) - added by pedromendonca 7 months ago.
Patch to add options to wp_sprintf_l()
49484-1.diff (4.0 KB) - added by pedromendonca 7 months ago.
Add revised patch with unnecessary spaces removed
49484-2.diff (3.9 KB) - added by pedromendonca 7 months ago.
Revised patch with fixed typo

Download all attachments as: .zip

Change History (12)

#1 @johnbillion
7 months ago

  • Component changed from I18N to Formatting
  • Keywords needs-patch added

Interesting idea.

#2 @pedromendonca
7 months ago

Some additional considerations

  1. Adding a third parameter to wp_sprintf() is not possible, because the current second parameter is ...$args, which has variable lenght and must be kept as the last parameter.
  1. A single wp_sprintf() can have multiple %l placeholders, with both ' and '/' or ', so it doesn't make sense to set it function wide.

The solution

Currently wp_sprintf() supports %l placeholder that sets a list ' and ' type delimited.
It's possible to add support for more placeholders:

  • %l$o - $o stands for OR - Outputs ' or ' delimiters
  • %l$a - $a stands for AND - Outputs ' and ' delimiters and is an alias for the default %l

The solution should allow all the below placeholder types:

  • Normal: %l$o, %l$a, %l (alias for %l$a)
  • Numbered: %2$l$o, %2$l$a, %2$l (alias for %2$l$a)

I've tested this successfully, I'll publish a patch below for everyone interested in testing this.

Last edited 7 months ago by pedromendonca (previous) (diff)

@pedromendonca
7 months ago

Patch to add options to wp_sprintf_l()

@pedromendonca
7 months ago

Add revised patch with unnecessary spaces removed

#3 @pedromendonca
7 months ago

  • Summary changed from Add option 'OR' to wp_sprintf() to Add option 'OR' to wp_sprintf_l()

#4 @pedromendonca
7 months ago

  • Keywords has-patch added; needs-patch removed

#5 @pedromendonca
7 months ago

Usage and testing

Normal and Numbered placeholders

wp_sprintf(
	'Test default: %l | Test OR: %l$o | Test AND: %l$a',
	array( 1, 3, 5, 7 ),
	array( 2, 4, 6, 8 ),
	array( 10, 20, 30, 40 )
);
wp_sprintf(
	'Test default: %1$l | Test OR: %2$l$o | Test AND: %3$l$a',
	array( 1, 3, 5, 7 ),
	array( 2, 4, 6, 8 ),
	array( 10, 20, 30, 40 )
);

Output: Test default: 1, 3, 5, and 7 | Test OR: 2, 4, 6, or 8 | Test AND: 10, 20, 30, and 40

Arrays of only 2 items

wp_sprintf(
	'Test default: %l | Test OR: %l$o | Test AND: %l$a',
	array( 1, 3 ),
	array( 2, 4 ),
	array( 10, 20 )
);

Output: Test default: 1 and 3 | Test OR: 2 or 4 | Test AND: 10 and 20

Last edited 7 months ago by pedromendonca (previous) (diff)

This ticket was mentioned in PR #161 on WordPress/wordpress-develop by pedro-mendonca.


7 months ago

This ticket was mentioned in Slack in #core-i18n by pedromendonca. View the logs.


7 months ago

@pedromendonca
7 months ago

Revised patch with fixed typo

This ticket was mentioned in Slack in #polyglots by pedromendonca. View the logs.


6 weeks ago

#9 @SergeyBiryukov
6 weeks ago

  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing
Note: See TracTickets for help on using tickets.