NOOP_Translations
云策文档标注
概述
NOOP_Translations 是 WordPress 中一个用于翻译的类,它实现了 Translations 接口,但通常不执行实际翻译,主要用于占位或测试。该类包含多个方法用于管理翻译条目和头部信息。
关键要点
- NOOP_Translations 类继承自 Translations 接口,提供翻译相关方法的空实现或默认行为。
- 主要方法包括设置和获取翻译头部(如 set_header、get_header)、翻译单数和复数字符串(如 translate、translate_plural)、管理翻译条目(如 add_entry、translate_entry)。
- 类属性包括 $entries(翻译条目数组)和 $headers(翻译头部数组),用于存储翻译数据。
- 方法如 select_plural_form 和 get_plural_forms_count 处理复数形式,translate_plural 根据计数返回单数或复数形式。
- merge_with 方法允许合并其他翻译对象,但当前实现为空。
代码示例
// 示例:使用 NOOP_Translations 进行翻译操作
$translations = new NOOP_Translations();
$translations->set_header('Language', 'en_US');
$header = $translations->get_header('Language'); // 返回 false
$translated = $translations->translate('Hello'); // 返回 'Hello'
$plural = $translations->translate_plural('apple', 'apples', 2); // 返回 'apples'注意事项
- NOOP_Translations 通常不用于生产环境,因为它不执行实际翻译,主要用于测试或作为占位符。
- 方法如 get_header 和 translate_entry 默认返回 false,需注意其返回值可能不符合预期。
- 在自定义翻译实现时,可参考此类结构,但需重写方法以提供实际功能。
原文内容
Methods
| Name | Description |
|---|---|
| NOOP_Translations::add_entry | – |
| NOOP_Translations::get_header | Returns a translation header. |
| NOOP_Translations::get_plural_forms_count | Returns the plural forms count. |
| NOOP_Translations::merge_with | Merges other translations into the current one. |
| NOOP_Translations::select_plural_form | Returns the plural form to use. |
| NOOP_Translations::set_header | Sets a translation header. |
| NOOP_Translations::set_headers | Sets translation headers. |
| NOOP_Translations::translate | Translates a singular string. |
| NOOP_Translations::translate_entry | Returns a given translation entry. |
| NOOP_Translations::translate_plural | Translates a plural string. |
Source
class NOOP_Translations {
/**
* List of translation entries.
*
* @since 2.8.0
*
* @var Translation_Entry[]
*/
public $entries = array();
/**
* List of translation headers.
*
* @since 2.8.0
*
* @var array<string, string>
*/
public $headers = array();
public function add_entry( $entry ) {
return true;
}
/**
* Sets a translation header.
*
* @since 2.8.0
*
* @param string $header
* @param string $value
*/
public function set_header( $header, $value ) {
}
/**
* Sets translation headers.
*
* @since 2.8.0
*
* @param array $headers
*/
public function set_headers( $headers ) {
}
/**
* Returns a translation header.
*
* @since 2.8.0
*
* @param string $header
* @return false
*/
public function get_header( $header ) {
return false;
}
/**
* Returns a given translation entry.
*
* @since 2.8.0
*
* @param Translation_Entry $entry
* @return false
*/
public function translate_entry( &$entry ) {
return false;
}
/**
* Translates a singular string.
*
* @since 2.8.0
*
* @param string $singular
* @param string $context
*/
public function translate( $singular, $context = null ) {
return $singular;
}
/**
* Returns the plural form to use.
*
* @since 2.8.0
*
* @param int $count
* @return int
*/
public function select_plural_form( $count ) {
return 1 === (int) $count ? 0 : 1;
}
/**
* Returns the plural forms count.
*
* @since 2.8.0
*
* @return int
*/
public function get_plural_forms_count() {
return 2;
}
/**
* Translates a plural string.
*
* @since 2.8.0
*
* @param string $singular
* @param string $plural
* @param int $count
* @param string $context
* @return string
*/
public function translate_plural( $singular, $plural, $count, $context = null ) {
return 1 === (int) $count ? $singular : $plural;
}
/**
* Merges other translations into the current one.
*
* @since 2.8.0
*
* @param Translations $other
*/
public function merge_with( &$other ) {
}
}