Search code examples
wordpresstranslationoutputshortcode

Translatable output in shortcode


Hello I would like to make part of the output translatable in my shortcode but I don't know how to do it. I tried several times but even if I managed to add the code, it was displayed outside of the div that outputs the variables so won't work..

My code without translation string is:

add_shortcode('cv', 'vp_cv');
function vp_cv($atts, $content=null) {
extract(shortcode_atts(array(
    'number' => 6
), $atts));
global $post;
$output .= '<div class="container">';
$query = new WP_Query('post_type=resume&posts_per_page=' . $number . '&cat=' . $categories);
while($query->have_posts() ) : $query->the_post();
    $year = get_post_meta($post->ID, 'resume_year', true);
    $title = get_the_title();
    $client = get_post_meta($post->ID, 'resume_client', true);
    $address = get_post_meta($post->ID, 'resume_address', true);
    $output .= '<p class="year">' . $year . '</p>';
    $output .= '<p class="cv-title">' . $title . '</p>';
    $output .= '<p class="cv-client"> <strong> Client:</strong> '  . $client . '</p>';
    $output .= '<p class="cv-address"> <strong> Address:</strong> ' . $address. '</p>';
    endwhile;
$output .= '</div>
<div class="clearboth"></div>';
return $output;
}

I'd like to add to the client and address a translatable string like: <?php _e('Client:','ikos');?> And it must result inside the tags

Thanks!


Solution

  • Assuming that you're loading text domain correctly, try this:

    <?php
      // ....
    
        $output .= '<p class="cv-client"> <strong> ' . __( 'Client: ', 'ikos' ) . ' </strong> '  . $client . '</p>';
        $output .= '<p class="cv-address"> <strong> ' . __( 'Address: ', 'ikos' ) . ' </strong> ' . $address. '</p>';
    
      // ....
    
    ?>
    

    Using __( 'Translatable string', 'your-text-domain' ); return the string translated without echo. Using _e( 'Translatable string', 'your-text-domain' ); echoes the translated string. Try it, hope it helps! If something is not clear feel free to ask.