Yii Framework Forum: Internationalization - Use Both Cphpmessagesource And Cdbmessagesource - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Internationalization - Use Both Cphpmessagesource And Cdbmessagesource Rate Topic: ***** 1 Votes

#1 User is offline   delux 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-November 12

Posted 13 November 2012 - 02:21 PM

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
1

#2 User is offline   delux 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-November 12

Posted 15 November 2012 - 05:10 PM

Nobody can help me ?

Not even a clue ?
1

#3 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 16 November 2012 - 04:18 AM

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),
        ), 

1

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 16 November 2012 - 05:17 AM

View Postyugene, on 16 November 2012 - 04:18 AM, said:

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)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
1

#5 User is offline   delux 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-November 12

Posted 18 November 2012 - 10:01 AM

Thank you very much to you both :)
0

#6 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 05 March 2013 - 11:14 AM

View Postyugene, on 16 November 2012 - 04:18 AM, said:

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),
        ), 



Thank`s a lot.
may ask you to show them in an simple example?
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
1

#7 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 05 March 2013 - 01:06 PM

Do you mean a call to these sources?
Just call Yii:t() with needed source, e.g.

Yii::t('test_db', 'my_message', array(), 'db_i18n');
Yii::t('test_file', 'my_message', array(), 'file_i18n');


Check out provided link to docs from my initial message.
0

#8 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 386
  • Joined: 08-June 10
  • Location:France (Ile-de-France/Val d'Oise)

Posted 05 March 2013 - 02:03 PM

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);
        }
    }}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users