钩子文档

ngettext

💡 云策文档标注

概述

ngettext 是一个 WordPress 过滤器钩子,用于过滤字符串的单数或复数形式,通常与国际化函数 _n() 结合使用。它允许开发者修改基于数字比较的翻译文本。

关键要点

  • ngettext 过滤器应用于处理复数形式的国际化函数 _n() 的翻译文本。
  • 参数包括 $translation(翻译文本)、$single(单数文本)、$plural(复数文本)、$number(比较数字)和 $domain(文本域)。
  • 此过滤器始终运行,即使国际化未启用或文本域未加载,可能导致性能问题。
  • 相关函数包括 _n() 用于翻译单复数形式,以及 gettext() 和 ngettext_with_context() 用于其他翻译场景。

注意事项

由于 ngettext 过滤器在任何情况下都会触发,开发者应谨慎挂钩以避免不必要的性能开销。


📄 原文内容

Filters the singular or plural form of a string.

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.
$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 plurals (<a href="https://developer.wordpress.org/reference/functions/_n/">_n()</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 context-specific translation functions such as <a href="https://developer.wordpress.org/reference/functions/_x/">_x()</a>, see filter hook gettext_with_context() and <a href="https://developer.wordpress.org/reference/hooks/ngettext_with_context/">ngettext_with_context()</a>.

Source

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

Changelog

Version Description
2.2.0 Introduced.