WordPress函数:国际化 _nx 翻译字符串,_n + _x

江河/ 04月16日/ WordPress/ 浏览 164

函数原型:


_nx( string $single, string $plural, int $number, string $context, string $domain = ‘default’ ): string


这个函数,从名字上就可以看出,是函数 _n 与 _x 的结合,支持上下文和复数。


使用gettext上下文,根据提供的数字翻译和检索单数或复数形式。


printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );


参数说明:


$single 如果数字是单数,则要使用的文本。


$plural 如果数字是复数,则要使用的文本。


$number 单数形式或复数形式的数字。


$context 为翻译人员提供上下文信息。


$domain 文本域。用于检索翻译字符串的唯一标识符。


函数源码:


function _nx( $single, $plural, $number, $context, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translation  = $translations->translate_plural( $single, $plural, $number, $context );

	$translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );

	$translation = apply_filters( "ngettext_with_context_{$domain}", $translation, $single, $plural, $number, $context, $domain );

	return $translation;
}


包含钩子:


apply_filters( ‘ngettext_with_context’, string $translation, string $single, string $plural, int $number, string $context, string $domain )

apply_filters( “ngettext_with_context_{$domain}”, string $translation, string $single, string $plural, int $number, string $context, string $domain )


发表评论

暂无评论,抢个沙发...

客服 工单