当前浏览:首页 / WordPress函数 / _e()

_e( string $text, string $domain = 'default' )

显示翻译文本

e 翻译more...

  • __() 检索$text的翻译
  • _e() 显示翻译文本
  • _ex() 使用gettext上下文显示翻译后的字符串
  • _n_noop() 在POT文件中注册多个字符串,但不翻译它们
  • _n() 基于提供的数字翻译和检索单数或复数形式
  • _nx_noop() 在POT文件中使用gettext上下文注册多个字符串,但不翻译它们
  • _nx() 使用gettext上下文,根据提供的数字检索单/复数形式进行翻译
  • _x() 使用gettext上下文检索已翻译的字符串
  • esc_attr__() 检索$text的翻译并将其转义,以便在属性中安全使用
  • esc_attr_e() 显示为在属性中安全使用而转义的翻译文本

esc 转义more...

  • esc_attr__() 检索$text的翻译并将其转义,以便在属性中安全使用
  • esc_attr_e() 显示为在属性中安全使用而转义的翻译文本
  • esc_attr_x() 使用gettext上下文翻译字符串,并将其转义以在属性中安全使用。
  • esc_attr() 转义HTML属性
  • esc_html__() 检索$text的翻译并将其转义,以便在HTML输出中安全使用。
  • esc_html_e() 显示为在HTML输出中安全使用而转义的翻译文本。
  • esc_html_x() 使用gettext上下文翻译字符串,并将其转义,以便在HTML输出中安全使用
  • esc_html() 转义HTML块
  • esc_js() 转义单引号、"、 、&,并修复行尾。
  • esc_textarea() 转义textarea值

参数

$text

(string) (必填) 要翻译的文本。

$domain

(string) (可选) Text domain(文本域),用于检索翻译字符串的唯一标识符。

默认值: 'default'



源码

查看源码 官方文档


更新日志

版本描述
1.2.0开始引入

使用示例

  • 示例1

    基本示例

    显示一些翻译文本:

    <?php _e( 'Some text to translate and display.', 'textdomain' ); ?>
    
  • 示例2

    WordPress文档指出您不应该使用

    echo __( 'translate text', 'textdomain' );

    但使用

    _e( 'translate text', 'textdomain' );

    替代

  • 示例3
    _e( string $text, string $domain = 'default' )

    在此函数中,“$domain”用于定位多语言文件。(主题翻译文件。)
    在wp-include中,有一个与此var相关的函数

    function load_theme_textdomain( $domain, $path = false ) {
    $locale = apply_filters( 'theme_locale', get_locale(), $domain );
     
    if ( ! $path )
    $path = get_template_directory();
     
    // Load the textdomain from the Theme provided location, or theme directory first
    $mofile = "{$path}/{$locale}.mo";
    if ( $loaded = load_textdomain($domain, $mofile) )
    return $loaded;
     
    // Else, load textdomain from the Language directory
    $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
    return load_textdomain($domain, $mofile);
    }

    你可以看到在这个文件中使用了$domain,实际上它是用来定位.mo文件的,所以你必须确保它与你的主题文件夹名称相同。

  • 示例4

    此函数与echo __相同,如:

    // their are same
    echo __( 'translate text', 'textdomain' );
    
    _e( 'translate text', 'textdomain' );
    
    
  • 示例5

    重要安全说明:_e()不是转义函数。由于所有输出确实需要转义,因此考虑改用__()

    例如,这是安全的:
    echo esc_html( __( 'This is my translatable text' ) )

    但这可能不是,因为输出可以被过滤,并且不会在输出位置附近转义:
    _e( 'This is my translatable text' )