wp_sprintf_l

函式
wp_sprintf_l ( $pattern, $args )
引數
  • (string) $pattern Content containing '%l' at the beginning.
    Required:
  • (array) $args List items to prepend to the content and replace '%l'.
    Required:
返回值
  • (string) Localized list items and rest of the content.
定義位置
相關方法
wp_sprintfwp_print_styleswp_script_iswp_scriptswp_install
引入
2.5.0
棄用
-

wp_sprintf_l。它是sprintf的一個封裝函式,用於用translate函式翻譯得到的字串: 該函式的工作原理與sprintf類似,但它也允許對返回的字串進行本地化。

在內容的其餘部分之前定位列表項。

‘%l’必須在第一個字元處,然後可以包含其餘的內容。列表項將有’、’、’、’、’和’,並根據$args引數中的列表項的數量新增。

function wp_sprintf_l( $pattern, $args ) {
	// Not a match.
	if ( '%l' !== substr( $pattern, 0, 2 ) ) {
		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( __( '%1$s, %2$s' ), '', '' ),
			/* translators: Used to join last two items in a list with more than 2 times. */
			'between_last_two' => sprintf( __( '%1$s, and %2$s' ), '', '' ),
			/* translators: Used to join items in a list with only 2 items. */
			'between_only_two' => sprintf( __( '%1$s and %2$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 );
}

常見問題

FAQs
檢視更多 >