translate_nooped_plural()
云策文档标注
概述
translate_nooped_plural() 函数用于翻译并返回已通过 _n_noop() 或 _nx_noop() 注册的单数或复数形式的字符串。它通常在已知对象数量后,处理可翻译的复数字符串时使用。
关键要点
- 函数接受一个由 _n_noop() 或 _nx_noop() 返回的数组作为参数,根据 $count 参数决定返回单数或复数形式的翻译文本。
- 如果 $nooped_plural 数组中包含文本域(domain)或上下文(context),函数会优先使用这些值进行翻译。
- 函数内部根据上下文是否存在,调用 _nx() 或 _n() 来完成实际的翻译工作。
代码示例
$message = _n_noop( '%s post', '%s posts', 'text-domain' );
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );注意事项
- 确保 $nooped_plural 参数是一个有效的数组,通常来自 _n_noop() 或 _nx_noop() 的返回值。
- 文本域参数 $domain 是可选的,默认值为 'default',但如果 $nooped_plural 数组中已指定文本域,则会覆盖此值。
- 函数自 WordPress 3.1.0 版本引入,适用于需要动态处理复数翻译的场景。
原文内容
Translates and returns the singular or plural form of a string that’s been registered with _n_noop() or _nx_noop() .
Description
Used when you want to use a translatable plural string once the number is known.
Example:
$message = _n_noop( '%s post', '%s posts', 'text-domain' );
...
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
Parameters
$nooped_pluralarrayrequired-
Array that is usually a return value from _n_noop() or _nx_noop() .
singularstringSingular form to be localized.pluralstringPlural form to be localized.contextstring|nullContext information for the translators.domainstring|nullText domain.
$countintrequired-
Number of objects.
$domainstringoptional-
Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains a text domain passed to _n_noop() or _nx_noop() , it will override this value. Default
'default'.More Arguments from _n_noop( … $domain )
Text domain. Unique identifier for retrieving translated strings.
Source
function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
if ( $nooped_plural['domain'] ) {
$domain = $nooped_plural['domain'];
}
if ( $nooped_plural['context'] ) {
return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
} else {
return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
}
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |