Translations
云策文档标注
概述
本文档介绍了 WordPress 中的 Translations 类,该类用于管理翻译条目和头部信息,支持单数和复数字符串的翻译操作。作为 PO 结构的基础类,它提供了添加、合并、获取和翻译条目的核心方法。
关键要点
- Translations 类包含两个主要属性:$entries(翻译条目数组)和 $headers(翻译头部数组)。
- 提供多种方法,如 add_entry、add_entry_or_merge 用于添加或合并条目,set_header、get_header 用于管理头部信息。
- translate 和 translate_plural 方法用于翻译单数和复数字符串,基于 Translation_Entry 对象。
- select_plural_form 和 get_plural_forms_count 方法处理复数形式逻辑,默认为英语规则(单数用索引 0,复数用索引 1)。
- merge_with 和 merge_originals_with 方法支持合并其他 Translations 对象的条目。
- 该类是 Gettext_Translations 等子类的基础,可被扩展以支持特定格式(如 MO/PO)。
代码示例
// 示例:使用 translate 方法翻译单数字符串
$translations = new Translations();
$translated_string = $translations->translate('Hello', 'context');
// 如果找到翻译,返回翻译后的字符串;否则返回原字符串 'Hello'注意事项
- add_entry 和 add_entry_or_merge 方法要求条目必须有有效的 key,否则返回 false。
- set_header 方法会覆盖已存在的头部值,且头部名称不应包含尾随冒号。
- translate_plural 方法依赖于 select_plural_form 来确定复数索引,子类可重写此方法以适配不同语言规则。
- merge_with 方法会直接覆盖当前条目,而 merge_originals_with 仅在条目不存在时添加或合并。
原文内容
Methods
| Name | Description |
|---|---|
| Translations::add_entry | Adds an entry to the PO structure. |
| Translations::add_entry_or_merge | Adds or merges an entry to the PO structure. |
| Translations::get_header | Returns a given translation header. |
| Translations::get_plural_forms_count | Returns the plural forms count. |
| Translations::merge_originals_with | Merges originals with existing entries. |
| Translations::merge_with | Merges other translations into the current one. |
| Translations::select_plural_form | Given the number of items, returns the 0-based index of the plural form to use |
| Translations::set_header | Sets $header PO header to $value |
| Translations::set_headers | Sets translation headers. |
| Translations::translate | Translates a singular string. |
| Translations::translate_entry | Returns a given translation entry. |
| Translations::translate_plural | Translates a plural string. |
Source
class 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();
/**
* Adds an entry to the PO structure.
*
* @since 2.8.0
*
* @param array|Translation_Entry $entry
* @return bool True on success, false if the entry doesn't have a key.
*/
public function add_entry( $entry ) {
if ( is_array( $entry ) ) {
$entry = new Translation_Entry( $entry );
}
$key = $entry->key();
if ( false === $key ) {
return false;
}
$this->entries[ $key ] = &$entry;
return true;
}
/**
* Adds or merges an entry to the PO structure.
*
* @since 2.8.0
*
* @param array|Translation_Entry $entry
* @return bool True on success, false if the entry doesn't have a key.
*/
public function add_entry_or_merge( $entry ) {
if ( is_array( $entry ) ) {
$entry = new Translation_Entry( $entry );
}
$key = $entry->key();
if ( false === $key ) {
return false;
}
if ( isset( $this->entries[ $key ] ) ) {
$this->entries[ $key ]->merge_with( $entry );
} else {
$this->entries[ $key ] = &$entry;
}
return true;
}
/**
* Sets $header PO header to $value
*
* If the header already exists, it will be overwritten
*
* TODO: this should be out of this class, it is gettext specific
*
* @since 2.8.0
*
* @param string $header header name, without trailing :
* @param string $value header value, without trailing n
*/
public function set_header( $header, $value ) {
$this->headers[ $header ] = $value;
}
/**
* Sets translation headers.
*
* @since 2.8.0
*
* @param array $headers Associative array of headers.
*/
public function set_headers( $headers ) {
foreach ( $headers as $header => $value ) {
$this->set_header( $header, $value );
}
}
/**
* Returns a given translation header.
*
* @since 2.8.0
*
* @param string $header
* @return string|false Header if it exists, false otherwise.
*/
public function get_header( $header ) {
return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false;
}
/**
* Returns a given translation entry.
*
* @since 2.8.0
*
* @param Translation_Entry $entry Translation entry.
* @return Translation_Entry|false Translation entry if it exists, false otherwise.
*/
public function translate_entry( &$entry ) {
$key = $entry->key();
return isset( $this->entries[ $key ] ) ? $this->entries[ $key ] : false;
}
/**
* Translates a singular string.
*
* @since 2.8.0
*
* @param string $singular
* @param string $context
* @return string
*/
public function translate( $singular, $context = null ) {
$entry = new Translation_Entry(
array(
'singular' => $singular,
'context' => $context,
)
);
$translated = $this->translate_entry( $entry );
return ( $translated && ! empty( $translated->translations ) ) ? $translated->translations[0] : $singular;
}
/**
* Given the number of items, returns the 0-based index of the plural form to use
*
* Here, in the base Translations class, the common logic for English is implemented:
* 0 if there is one element, 1 otherwise
*
* This function should be overridden by the subclasses. For example MO/PO can derive the logic
* from their headers.
*
* @since 2.8.0
*
* @param int $count Number of items.
* @return int Plural form to use.
*/
public function select_plural_form( $count ) {
return 1 === (int) $count ? 0 : 1;
}
/**
* Returns the plural forms count.
*
* @since 2.8.0
*
* @return int Plural forms count.
*/
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 ) {
$entry = new Translation_Entry(
array(
'singular' => $singular,
'plural' => $plural,
'context' => $context,
)
);
$translated = $this->translate_entry( $entry );
$index = $this->select_plural_form( $count );
$total_plural_forms = $this->get_plural_forms_count();
if ( $translated && 0 <= $index && $index < $total_plural_forms &&
is_array( $translated->translations ) &&
isset( $translated->translations[ $index ] ) ) {
return $translated->translations[ $index ];
} else {
return 1 === (int) $count ? $singular : $plural;
}
}
/**
* Merges other translations into the current one.
*
* @since 2.8.0
*
* @param Translations $other Another Translation object, whose translations will be merged in this one (passed by reference).
*/
public function merge_with( &$other ) {
foreach ( $other->entries as $entry ) {
$this->entries[ $entry->key() ] = $entry;
}
}
/**
* Merges originals with existing entries.
*
* @since 2.8.0
*
* @param Translations $other
*/
public function merge_originals_with( &$other ) {
foreach ( $other->entries as $entry ) {
if ( ! isset( $this->entries[ $entry->key() ] ) ) {
$this->entries[ $entry->key() ] = $entry;
} else {
$this->entries[ $entry->key() ]->merge_with( $entry );
}
}
}
}