2016-10-14 21 views
1

在我的项目中,我正尝试在我的一个模块中提供动态大小调整。我在yii2国际化中想弄清楚如何获得翻译文本的字符串长度。Yii2如何获得翻译消息的长度

例如:

<?php 
     //I am getting the name from the database. Assume name to be "Hello" 
     $name = $gettingNameFrom->db; 

     //Now $name is equal to string "Hello" 

     //The below function will dump the output to be int(5) as the length of hello is 5 
     var_dump(strlen($name)); 

     //Now I want to apply translation to the above name in the db. 
     // I have all my translation configured and working fine. 

     echo Yii::t('app','{0}',[$name]); 
     //I have configured fo french language. 
     // the above output for "Hello" in french would be "Bonjour". 



    ?> 

现在,我怎么得到翻译文本的长度是多少?我无法在这个主题上找到任何帮助。任何帮助赞赏。

谢谢!

回答

2
$translated = Yii::t('app', $name); 
var_dump(strlen($translated)); 
+0

不,它没有帮助。它获得**“字符串(37)”的长度@MISSING:app.Hello FOR LANGUAGE fr @“”**。但我的翻译文件中提供了翻译。当我切换我的语言时,它将文本从Hello更改为Bonjour。 –

+0

试着用'$ translated = Yii :: t('app',$ name);' – Bizley