在PHP中,特别是在wordpress中,__(‘string’)和_x(‘string’)之间的区别是什么?
我正在阅读wordpress文档并感到困惑.从WordPress doc上的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' )
);
解决方法:
在wordpress中,_x()和__()都是翻译函数. _x()允许您指定翻译的上下文:
<?PHP _x( $text, $context, $domain ); ?>
而__()不:
<?PHP __( $text, $domain ); ?>
上下文选项对于字面翻译可能无法产生所需结果的各种情况非常有用.
Since the string ‘Read’ on its own Could have one of several different meanings in English, context is given so that translators kNow that they should be supplying a short term that means “Books I have read.”
其他一些例子可能是:“日期”,“类型”,“正确”,“离开”等.
资料来源:WordPress Codex
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。