Internationalization - Use Both Cphpmessagesource And Cdbmessagesource

Hi everybody,

I would like to know if it is possible to use CPhpMessageSource and CDbMessageSource at the same time.

I want to use CPhpMessageSource for static content and CDbMessageSource for dynamic content.

Thanks for your help.

deLux

Nobody can help me ?

Not even a clue ?

Yii::t() documentaiton

Set $source parameter to what you need in each exact case: in your config file configure two message components - one with files source and one with db source.

You may write two helper functions in which you’ll call Yii::t() with exact source. Seems, that’s all you need.

Components configuration will look something like (db source is taken from my code, file source is not tested, but should be something like that):





        'db_i18n' => array(

            'class' => 'DbMessageSource',

            'connectionID' => 'db',

            'sourceMessageTable' => 'source',

            'translatedMessageTable' => 'translation',

            'cachingDuration' => (YII_DEBUG ? 0 : 360),

        ), 

        'file_i18n' => array(

            'class' => 'CPhpMessageSource',

            'basePath' => 'YOUR PATH',

            'cachingDuration' => (YII_DEBUG ? 0 : 3600),

        ), 



In addition you could may use the ‘source’ paramater of function t to use the desirable class

t(string $category, string $message, array $params=array ( ), string $source=‘your class’, string $language=NULL)

Thank you very much to you both :)

Thank`s a lot.

may ask you to show them in an simple example?

Do you mean a call to these sources?

[size=2]Just call Yii:t() with needed source, e.g.[/size]

[size=2]Yii::t(‘test_db’, ‘my_message’, array(), ‘db_i18n’[/size][size=2]);[/size]

Yii::t(‘test_file’, ‘my_message’, array(), ‘file_i18n’[size=2]);[/size]

[size=2]

[/size]

[size=2]Check out provided link to docs from my initial message.[/size]

An alternative method is to create your own message source class. In your case you’ld have to set up some kind of Chain of Responsibility .

Here is what I did to make the MessageSource first look in the Theme and then in the default path. It is just a matter of overloading getMessageFile in this case:


/**

 * PHP Message source allowing messages to be specified in the theme.

*/

class ThemedPhpMessageSource extends CPhpMessageSource {

    private $_files;


    /**

 	* (non-PHPdoc)

 	* @see CPhpMessageSource::getMessageFile()

 	*/

    protected function getMessageFile($category, $language) {

        if(!isset($this->_files[$category][$language]))

        {

            if(($pos=strpos($category,'.'))!==false)

            {

                $moduleClass=substr($category,0,$pos);

                $moduleCategory=substr($category,$pos+1);

                if($moduleClass==='theme') {

                    $filename=Yii::app()->theme->getBasePath().DIRECTORY_SEPARATOR.'messages'.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$moduleCategory.'.php';

                    if(file_exists($filename)) {

                        $this->_files[$category][$language]=$filename;

                    } else {

                        return;

                    }

                }

            }

        }

        if(isset($this->_files[$category][$language])) {

            return $this->_files[$category][$language];

        } else {

            return parent::getMessageFile($category,$language);

        }

    }}