Translation_Entry
云策文档标注
概述
Translation_Entry 是 WordPress 中用于管理翻译条目的类,包含字符串、复数形式、翻译、上下文等属性。它提供了构造函数、唯一键生成和合并功能,支持国际化开发。
关键要点
- Translation_Entry 类用于表示单个翻译条目,包含如 $singular、$plural、$translations 等属性。
- __construct() 方法通过数组参数初始化条目,设置 $is_plural 等属性,并确保数组类型正确。
- key() 方法生成条目的唯一键,基于上下文和单数形式,用于 MO 文件格式。
- merge_with() 方法合并另一个 Translation_Entry 对象,整合 flags、references 和 extracted_comments。
- Translation_Entry() 是已弃用的 PHP4 构造函数,自 5.4.0 起应使用 __construct()。
代码示例
// 创建 Translation_Entry 实例
$args = array(
'singular' => 'Hello World',
'plural' => 'Hello Worlds',
'translations' => array('你好世界', '你好世界们'),
'context' => 'greeting'
);
$entry = new Translation_Entry($args);
// 生成唯一键
$key = $entry->key();
// 合并另一个条目
$other_entry = new Translation_Entry(array('singular' => 'Test'));
$entry->merge_with($other_entry);注意事项
- 如果 $args 中未设置 'singular',构造函数会返回空对象,不初始化属性。
- key() 方法在 $singular 为 null 时返回 false,确保条目有效。
- Translation_Entry() 构造函数已弃用,建议使用 __construct() 以避免兼容性问题。
- merge_with() 方法会去重 flags 和 references,并拼接 extracted_comments。
原文内容
Methods
| Name | Description |
|---|---|
| Translation_Entry::__construct | – |
| Translation_Entry::key | Generates a unique key for this entry. |
| Translation_Entry::merge_with | Merges another translation entry with the current one. |
| Translation_Entry::Translation_Entry | PHP4 constructor. — deprecated |
Source
class Translation_Entry {
/**
* Whether the entry contains a string and its plural form, default is false.
*
* @var bool
*/
public $is_plural = false;
public $context = null;
public $singular = null;
public $plural = null;
public $translations = array();
public $translator_comments = '';
public $extracted_comments = '';
public $references = array();
public $flags = array();
/**
* @param array $args {
* Arguments array, supports the following keys:
*
* @type string $singular The string to translate, if omitted an
* empty entry will be created.
* @type string $plural The plural form of the string, setting
* this will set `$is_plural` to true.
* @type array $translations Translations of the string and possibly
* its plural forms.
* @type string $context A string differentiating two equal strings
* used in different contexts.
* @type string $translator_comments Comments left by translators.
* @type string $extracted_comments Comments left by developers.
* @type array $references Places in the code this string is used, in
* relative_to_root_path/file.php:linenum form.
* @type array $flags Flags like php-format.
* }
*/
public function __construct( $args = array() ) {
// If no singular -- empty object.
if ( ! isset( $args['singular'] ) ) {
return;
}
// Get member variable values from args hash.
foreach ( $args as $varname => $value ) {
$this->$varname = $value;
}
if ( isset( $args['plural'] ) && $args['plural'] ) {
$this->is_plural = true;
}
if ( ! is_array( $this->translations ) ) {
$this->translations = array();
}
if ( ! is_array( $this->references ) ) {
$this->references = array();
}
if ( ! is_array( $this->flags ) ) {
$this->flags = array();
}
}
/**
* PHP4 constructor.
*
* @since 2.8.0
* @deprecated 5.4.0 Use __construct() instead.
*
* @see Translation_Entry::__construct()
*/
public function Translation_Entry( $args = array() ) {
_deprecated_constructor( self::class, '5.4.0', static::class );
self::__construct( $args );
}
/**
* Generates a unique key for this entry.
*
* @since 2.8.0
*
* @return string|false The key or false if the entry is null.
*/
public function key() {
if ( null === $this->singular ) {
return false;
}
// Prepend context and EOT, like in MO files.
$key = ! $this->context ? $this->singular : $this->context . "4" . $this->singular;
// Standardize on n line endings.
$key = str_replace( array( "rn", "r" ), "n", $key );
return $key;
}
/**
* Merges another translation entry with the current one.
*
* @since 2.8.0
*
* @param Translation_Entry $other Other translation entry.
*/
public function merge_with( &$other ) {
$this->flags = array_unique( array_merge( $this->flags, $other->flags ) );
$this->references = array_unique( array_merge( $this->references, $other->references ) );
if ( $this->extracted_comments !== $other->extracted_comments ) {
$this->extracted_comments .= $other->extracted_comments;
}
}
}