[已解决]如何指定使用MessageSource

如何指定使用CPhpMessageSource, 还是CGettextMessageSource?

能不能指定messages的路径和sourceLanguage? 那应该怎么做呢?

另外, 在命令行里面  yiic message的fileTypes里, 第一次指定 'php', 已经成功了. 又另加了一个xml, 可是并没有生成xml的翻译文件.  如果我想生成gettext的翻译文件, 我是不是可以指定 po?

谢谢!

你在app config里配置'messages' components,



'components'=>array(


      'messages'=>array(


            'class'=>'CGettextMessageSource',


      ),


)


要设置message路径和sourceLanguage,只要在上面的数组(里面一层)设置就可以了。

yiic message生成的是CPhpMessage使用的message。

现在我设置的language, 已经可以使下面的日志在 "Application Log"和"程序日志" 切换成功. 可是我使用 Yii::t('default', 'Home') 却没有按预期的语言转换?

检查Yii::app()->getLanguage() 已经正常输出 en_us和zh_cn, 也已经使用yii message生成了正确的目录和文件.

还有什么地方需要我再做呢?

protected/messages/zh_cn/default.php有么?

有的. 是使用 yiic message 生成的. 里面的数组也是  索引-值 的方式.

能把你的app config贴出来看看么?

对于语言的设置目前相关的工作有:

  1. 通过用户的浏览器或COOKIE或SESSION或用户的设置里里, 得到zh_cn或en_us, 指定语言:

Yii::app()->setLanguage($locale);

  1. 在 config/main.php的中加入

'components'=>array(

      'messages'=>array(

            'class'=>'CPhpMessageSource',

      ),

)

  1. 所有的标签使用 Yii::t('XXX', 'XXXX')的方式

  2. 使用 yiic messsage生成 php message 文件.



<?php





return array(


	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',


	'name'=>'XXXXXX',


	'defaultController'=>'default',





	// preloading 'log' component


	'preload'=>array('log'),





	// autoloading model and component classes


	'import'=>array(


		'application.models.*',


		'application.components.*',


	),





	// application-level parameters that can be accessed


	// using Yii::app()->params['paramName']


	'params'=>require(dirname(__FILE__).'/params.php'),





	// application components


	'components'=>array(


	    'session' => array(


	        'autoStart' => true,


	    ),


		'log'=>array(


			'class'=>'CLogRouter',


			'routes'=>array(


				array(


					'class'=>'CFileLogRoute',


					'levels'=>'error, warning',


				),


				array(


                    'class'=>'CWebLogRoute',


                    'levels'=>'trace',


                ),


                array(


                    'class'=>'CProfileLogRoute',


                    'report'=>'summary',


                    'levels'=>'profile',


                ),


			),


		),


		'user'=>array(


			'allowAutoLogin'=>true,


			'loginUrl'=>null,


		),


		'db'=>array(


		    //uncomment the following to use Sqllite as database


			//'connectionString'=>'sqlite:'.dirname(__FILE__).'/../data/blog.db',


			'connectionString'=>'mysql:host=localhost;dbname=xxx_dev',


			'username'=>'root',


			'password'=>'root',


		    'charset' => 'utf8',


		),


		'urlManager'=>array(


			'urlFormat'=>'path',


		    'urlSuffix'=>'.html',


		    'showScriptName'=>false,


			'rules'=>array(


		        '/'   => 'default/index',


				'tag/<tag>'=>'post/list',


				'posts'=>'post/list',


				'post/<id:d+>'=>'post/show',


				'post/update/<id:d+>'=>'post/update',


		        'people/profile/<id:w+>' => 'people/profile'


			),


		),


		


	),


	'messages'=>array(


            'class'=>'CPhpMessageSource',


		    'sourceLanguage' => 'en_us',


        ),


);


messages/zh_cn/default.php:



<?php


return array (


  '--Select--' => '--选择--',


  'Administrator' => '管理员',


  'Close' => '关闭',


  'Home' => '首页',


);


CPhpMessage没有sourceLanguage属性的(只有language属性)。

我刚刚用你的配置测试过,没问题啊。我在app config里设置了'language'=>'zh_cn',并且创建了文件protected/messages/zh_cn/default.php,里面的内容是:

return array(

  'test'=>'测试',

);

然后我在layout里显示:Yii::t('default','test')

现在跟踪到为什么不能 work:

CPhpMessageSource.php 的 第72行:

$messageFile=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';

$this->basePath 为空. 致使第81行的文件找不到.

如果在第72行代码前指定 $this->basePath的路径, 这个问题就解决了

所以, 我的下一个问题: 在哪里指定这个 $this->basePath呢?

你设置过basePath属性么?如果没有设置的话,CPhpMessageSource在init()里会自动把basePath设成Yii::getPathOfAlias('application.messages'),也就是protected/messages

我只能说我从来没有有意的去设置了它.  可能在代码的什么地方引起了冲突.

我应该怎么样去找到这个源头, 或是重新设置basePath呢?

你看看有没有这样的代码:Yii::app()->messages->basePath=…;

找不到的话,你在app config里设置一下messages的basePath属性,把它设成Yii::getPathOfAlias('application.messages')

没有搜索到. 使用Yii::getPathOfAlias('application.messages')也是不行.

看来是在程序里加载完配置以后的某些地方出了问题.

知道答案了, 所以就不怕了. 目前也有临时解决方案.

谢谢强哥!

Hi Qiang,

关于这个basePath的问题,



        //     path/to/Yii/base/CApplication.php 第108行


        public function __construct($config=null)


	{


		Yii::setApplication($this);


		$this->initSystemHandlers();


		$this->registerCoreComponents();


		$this->configure($config);


		$this->init();


	}


$this->registerCoreComponents();

这一步就已经注册了 CPhpMessageSource, 并使用了它的init()方法设置basePath,

$this->basePath=Yii::getPathOfAlias('application.messages');

$this->configure($config);

才初始化 Yii::setPathOfAlias('application',$this->getBasePath());

所以, 我想这个是得不到  basePath 的原因吧。

有道理。虽然registerCoreComponents()并不会创建messages,但是如果在configure之前调用Yii::t()的话,messages还是会被创建,然后就出问题了。我会解决这个问题的。谢谢!