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

2020-04-16 08:24发布

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

And then how do we pull the strings into a PO file?

4条回答
beautiful°
2楼-- · 2020-04-16 08:58

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

查看更多
疯言疯语
3楼-- · 2020-04-16 09:00

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.

查看更多
霸刀☆藐视天下
4楼-- · 2020-04-16 09:02

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

查看更多
forever°为你锁心
5楼-- · 2020-04-16 09:12

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

查看更多
登录 后发表回答