Yii: GetText _() vs Yii::t()

前端 未结 4 1103
温柔的废话
温柔的废话 2021-01-21 18:32

When translating strings in Yii with GetText, do we have to use Yii::t($category,\'message\') or gettext\'s _(\'message\') syntax?

And then how

相关标签:
4条回答
  • 2021-01-21 19:10

    I always use Yii::t('xx','yy') in order to have more control on localization. I can split in more file under

    message/LANG/file.php
    

    with

    <?php
      return array(
        'xx' => 'localized',
      );
    ?>
    

    This for Yii Framework 1.x

    0 讨论(0)
  • 2021-01-21 19:10

    I use it like this:

    when i have for example:

    Yii::app()->language = en
    

    in folder messages have I folder en and on this folder I have file lang.php

    on this folder lang.php I have for example:

    <?php
        return array(
            'example1'=>'search',
            'example2'=>'news',
        );
    

    then when i need call view translate:

    <?=Yii::t('lang','example1');?>
    

    and result is: search


    when I change: Yii::app()->language = de, I must have messages/de/lang.php and on this foler I have:

    <?php
        return array(
            'example1'=>'Suche',
            'example2'=>'Nachrichten',
        );
    

    then when i need call view translate:

    <?=Yii::t('lang','example1');?>
    

    result is: Suche

    0 讨论(0)
  • 2021-01-21 19:19

    You need to use Yii::t() for translations. with the inbuilt yiic message command you can generate all the text in to language file.

    Example-

     Yii::t('app', 'Hello World');
     Yii::t('email', 'Welcome');
    

    This will generate 2 files app.php and email.php inside your messages directory. messages/en/app.php & messages/en/email.php

    please find more info about Yii translations here

    https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n https://www.yiiframework.com/doc/guide/1.1/en/topics.i18n

    0 讨论(0)
  • 2021-01-21 19:22

    You can put strings via POEdit app: http://poedit.net/ It allow you to scan your project files and automatically add all required strings into .po file.

    If it comes to difference between Yii::t($category,'message') and _('message') - you should use Yii::t($category,'message'). GetText's _('message') works with a quite different po/mo catalog structure.

    I personally use POEdit as string scanner and it works like a charm.

    0 讨论(0)
提交回复
热议问题