WordPress函数:esc_html_x 带上下文的转义翻译

江河/ 04月25日/ WordPress/ 浏览 111

函数原型:


esc_html_x( string $text, string $context, string $domain = ‘default’ ): string


使用gettext上下文翻译字符串,并对其进行转义,以便在HTML输出中安全使用。


如果没有翻译,或者没有加载文本域,则会转义并返回原始文本。


参数说明:


$text 要转义的文字


$context 上下文


$domain 文本域。用于检索翻译字符串的唯一标识符。


函数源码:


function esc_html_x( $text, $context, $domain = 'default' ) {
	return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
}


使用举例:


<!-- Here, we're asking the user to comment (verb): -->
<a href="#comment">
  <?php 
    echo esc_html_x( 
      'Comment', 
      'Verb: To leave a comment', // Here's the contextual help
      'wpdocs_my_theme' 
    ); 
  ?>
</a>
<!-- Here, we're simply adding a heading with the text "Comment" (noun) -->
<h3>
  <?php 
    echo esc_html_x( 
      'Comment', 
      'Noun: An individual comment', // Here's the contextual help
      'wpdocs_my_theme' 
    ); 
  ?>
</h3>


发表评论

暂无评论,抢个沙发...

客服 工单