Jos*_*sen 6 php syntax wordpress
__('string')在 PHP 中,特别是在 Wordpress 中,和 之间有什么区别_x('string')?
我正在阅读 WordPress 文档并感到困惑。以下很好的混淆示例取自 Wordpress 文档中的示例代码register_post_type():
$labels = array(
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
);
Run Code Online (Sandbox Code Playgroud)
在WordPress中, 和_x()都是__()翻译功能。_x()允许您指定翻译的上下文:
<?php _x( $text, $context, $domain ); ?>
Run Code Online (Sandbox Code Playgroud)
而__()没有:
<?php __( $text, $domain ); ?>
Run Code Online (Sandbox Code Playgroud)
上下文选项对于直译可能无法产生所需结果的各种情况很有用。
由于字符串“Read”本身在英语中可能具有几种不同含义之一,因此给出了上下文,以便翻译人员知道他们应该提供一个简短的术语,表示“我读过的书”。
其他一些示例可能是:“日期”、“类型”、“正确”、“叶子”等。
来源:WordPress 法典