wp_sprintf_l

Localize list items before the rest of the content.

Description

(string) wp_sprintf_l( (string) $pattern, (array) $args ); 

The %l must be at the first characters can then contain the rest of the content. The list items will have , ,, , and, and and added depending on the amount of list items in the $args parameter.

Returns (string)

Localized list items and rest of the content.

Parameters (2)

0. $pattern (string)
Content containing %l at the beginning.
1. $args (array)
List items to prepend to the content and replace %l..

Usage

  1. if ( !function_exists( 'wp_sprintf_l' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Content containing '%l' at the beginning. 
  5. $pattern = ''; 
  6.  
  7. // List items to prepend to the content and replace '%l'. 
  8. $args = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_sprintf_l($pattern, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_sprintf_l( $pattern, $args ) { 
  2. // Not a match 
  3. if ( substr($pattern, 0, 2) != '%l' ) 
  4. return $pattern; 
  5.  
  6. // Nothing to work with 
  7. if ( empty($args) ) 
  8. return ''; 
  9.  
  10. /** 
  11. * Filters the translated delimiters used by wp_sprintf_l(). 
  12. * Placeholders (%s) are included to assist translators and then 
  13. * removed before the array of strings reaches the filter. 
  14. * Please note: Ampersands and entities should be avoided here. 
  15. * @since 2.5.0 
  16. * @param array $delimiters An array of translated delimiters. 
  17. */ 
  18. $l = apply_filters( 'wp_sprintf_l', array( 
  19. /** translators: used to join items in a list with more than 2 items */ 
  20. 'between' => sprintf( __('%s, %s'), '', '' ),  
  21. /** translators: used to join last two items in a list with more than 2 times */ 
  22. 'between_last_two' => sprintf( __('%s, and %s'), '', '' ),  
  23. /** translators: used to join items in a list with only 2 items */ 
  24. 'between_only_two' => sprintf( __('%s and %s'), '', '' ),  
  25. ) ); 
  26.  
  27. $args = (array) $args; 
  28. $result = array_shift($args); 
  29. if ( count($args) == 1 ) 
  30. $result .= $l['between_only_two'] . array_shift($args); 
  31. // Loop when more than two args 
  32. $i = count($args); 
  33. while ( $i ) { 
  34. $arg = array_shift($args); 
  35. $i--; 
  36. if ( 0 == $i ) 
  37. $result .= $l['between_last_two'] . $arg; 
  38. else 
  39. $result .= $l['between'] . $arg; 
  40. return $result . substr($pattern, 2);