在其余内容之前本地化列表项。
原型
wp_sprintf_l( string $pattern, array $args )
描述
‘%l’必须位于第一个字符,然后才能包含其余内容。列表项将添加’,’,‘和’,以及’和’,具体取决于$ args参数中的列表项数量。
参数
$pattern
(string)
(Required)
开头包含’%l’的内容。
$args
(array)
(Required)
列出要添加到内容中的项目并替换’%l’。
返回值
(string)
本地化列表项和其他内容。
源文件
路径:wp-includes/formatting.php
<?php
...
function wp_sprintf_l( $pattern, $args ) {
// Not a match
if ( substr($pattern, 0, 2) != '%l' )
return $pattern;
// Nothing to work with
if ( empty($args) )
return '';
/**
* Filters the translated delimiters used by wp_sprintf_l().
* Placeholders (%s) are included to assist translators and then
* removed before the array of strings reaches the filter.
*
* Please note: Ampersands and entities should be avoided here.
*
* @since 2.5.0
*
* @param array $delimiters An array of translated delimiters.
*/
$l = apply_filters( 'wp_sprintf_l', array(
/* translators: used to join items in a list with more than 2 items */
'between' => sprintf( __('%s, %s'), '', '' ),
/* translators: used to join last two items in a list with more than 2 times */
'between_last_two' => sprintf( __('%s, and %s'), '', '' ),
/* translators: used to join items in a list with only 2 items */
'between_only_two' => sprintf( __('%s and %s'), '', '' ),
) );
$args = (array) $args;
$result = array_shift($args);
if ( count($args) == 1 )
$result .= $l['between_only_two'] . array_shift($args);
// Loop when more than two args
$i = count($args);
while ( $i ) {
$arg = array_shift($args);
$i--;
if ( 0 == $i )
$result .= $l['between_last_two'] . $arg;
else
$result .= $l['between'] . $arg;
}
return $result . substr($pattern, 2);
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_sprintf_l/