最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xgettext - Localization inside shortcode not working

programmeradmin0浏览0评论

I use a shortcode that retrieves the url of woocommerce products and builds a link:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
    $text = 'Some text';
        return '<p class="woo-booking"><a href="'.$html.'">'.$text.'</a></p>';
}

Works fine. But now I need the text translated like this:

esc_html_e( 'Some text', 'generatepress-child' );

So, the code would be:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
        return '<p class="woo-booking"><a href="'.$html.'">'.esc_html_e( 'Some text', 'generatepress-child' ).'</a></p>';
}

Language files are there and working. But not with this shortcode. The string is translated but appears at the top of the page and is repeated several times. What exactly am I missing, and how can I fix this?

Thank you!

I use a shortcode that retrieves the url of woocommerce products and builds a link:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
    $text = 'Some text';
        return '<p class="woo-booking"><a href="'.$html.'">'.$text.'</a></p>';
}

Works fine. But now I need the text translated like this:

esc_html_e( 'Some text', 'generatepress-child' );

So, the code would be:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
        return '<p class="woo-booking"><a href="'.$html.'">'.esc_html_e( 'Some text', 'generatepress-child' ).'</a></p>';
}

Language files are there and working. But not with this shortcode. The string is translated but appears at the top of the page and is repeated several times. What exactly am I missing, and how can I fix this?

Thank you!

Share Improve this question asked Oct 8, 2020 at 10:06 user2516117user2516117 771 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

esc_html_e() displays (echo) the output, hence it appears at the wrong place. So you should instead use esc_html__() which returns the translated string.

发布评论

评论列表(0)

  1. 暂无评论