钩子文档

ngettext_with_context

💡 云策文档标注

概述

ngettext_with_context 是一个 WordPress 过滤器钩子,用于在 gettext 上下文中过滤字符串的单数或复数形式。它通常与 _nx() 函数结合使用,处理翻译时的上下文和复数形式。

关键要点

  • 过滤器应用于翻译文本,参数包括 $translation(翻译后的文本)、$single(单数文本)、$plural(复数文本)、$number(数字)、$context(上下文)和 $domain(文本域)。
  • 此钩子始终运行,即使国际化未启用或文本域未加载,可能导致性能问题。
  • 相关函数包括 _nx()(用于带上下文的单复数翻译)、_n()(单复数翻译)和 gettext_with_context(上下文翻译过滤器)。

注意事项

  • 注意性能影响:由于过滤器总是执行,如果钩子函数过多,可能影响网站速度。
  • 正确使用参数:确保传递正确的 $number、$context 和 $domain 以匹配翻译需求。

📄 原文内容

Filters the singular or plural form of a string with gettext context.

Parameters

$translationstring
Translated text.
$singlestring
The text to be used if the number is singular.
$pluralstring
The text to be used if the number is plural.
$numberint
The number to compare against to use either the singular or plural form.
$contextstring
Context information for the translators.
$domainstring
Text domain. Unique identifier for retrieving translated strings.

More Information

This filter hook is applied to the translated text by the internationalization function that handle contexts and plurals at the same time (<a href="https://developer.wordpress.org/reference/functions/_nx/">_nx()</a>).

IMPORTANT: This filter is always applied even if internationalization is not in effect, and if the text domain has not been loaded. If there are functions hooked to this filter, they will always run. This could lead to a performance problem.

For regular translation functions such as <a href="https://developer.wordpress.org/reference/functions/_e/">_e()</a>, and for examples on usage, see <a href="https://developer.wordpress.org/reference/hooks/gettext/">gettext()</a>.

For singular/plural aware translation functions such as <a href="https://developer.wordpress.org/reference/functions/_n/">_n()</a>, see <a href="https://developer.wordpress.org/reference/hooks/ngettext/">ngettext()</a>.

For context-specific translation functions such as <a href="https://developer.wordpress.org/reference/functions/_x/">_x()</a>, see filter hook gettext_with_context().

Source

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

Changelog

Version Description
2.8.0 Introduced.