在WordPress中使用html标签和数字对字符串进行国际化

时间:2017-02-07 10:34:26

标签: wordpress localization

如何翻译带有标签的字符串。

假设有一个像

这样的字符串
$a = 5;
There are <?php echo $a; ?> <span>people</span> in this country.

Javascript文件:

wp_localize_script( 'store-locator', 'storelocatorjstext', array(
    'nostores'   => __( 'There are 5 people in this country.', 'textdomain' )
) );

将此类案件国际化的正确方法是什么?

我的方法:

<?php _e('There are 5 <span>people</span> in this country.', 'textdomain'); ?>

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

对于Html文件:

<?php
$a = 5;
echo sprintf( __ ('There are %s <span>people</span> in this country.','textdomain' ), $a);
?>

对于Javascript文件:

<?php
$a = 5;
$message = sprintf( __ ('There are %s <span>people</span> in this country.','textdomain' ), $a);
wp_localize_script( 'store-locator', 'storelocatorjstext', array(
    'nostores'   => $message
) );
?>

当你在JS中获得变量时:

console.log( storelocatorjstext.nostores );

输出:

There are 5 <span>people</span> in this country.

答案 1 :(得分:1)

在我看来,这是写这样的字符串的方式。

您可以根据需要添加'return'代替'echo'。

<?php 
$a = 5;
echo sprintf('There are %s <span>people</span> in this country.', $a); 
?>