Yii Framework Forum: I18N For Database Data - Yii Framework Forum

Jump to content

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

I18N For Database Data Rate Topic: -----

#1 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 08 October 2012 - 03:47 AM

I used the i18n in my application for menu and static contents. Now i would like to do this on database content. i have a table named 'about' with field id and description. I want to display the description in index.php page. I have no idea about coding to display this description and want to change the description based on language selected in dropdown list (used the language picker extension).Need the code for display description from database and change the data based on language.
Thanks
Aneesh.
0

#2 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 08 October 2012 - 07:42 AM

View PostAneesh Asokan, on 08 October 2012 - 03:47 AM, said:

I used the i18n in my application for menu and static contents. Now i would like to do this on database content. i have a table named 'about' with field id and description. I want to display the description in index.php page. I have no idea about coding to display this description and want to change the description based on language selected in dropdown list (used the language picker extension).Need the code for display description from database and change the data based on language.



1. add language code table to your 'about' table.

id, description, language_id (fk)

2. populate with proper data

1 - this is a description - english
2 - ceci est une description - french
3 - bla bla bla - other language

3. in the app, you need to do a couple of things.

check for the language to be displayed and then fetch the description in the proper language.

To setup i18n, you need to do the following. I will show you what i did in my current project.

in main.php, set default language.

return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	// Application name
        'name'=>'PMMetriX',
        'sourceLanguage'=>'en_us',


in userIdentity, i set a session variable to the default language selected by the user

                             // set language code as per user preferrence - i.e. en_us, fr_ca,, etc. 
                                $languageCode = Language::model()->findByPk($user->language_id);
                                $app->user->setState('languageId', $languageCode->code);
      


i extended Controller so that everytime an action is requested, it populates Yii::app->user->languageId

    public function init()
    {
        if (isset(Yii::app()->user->languageId)) 
            return Yii::app()->language = Yii::app()->user->languageId;
    }  


i then created a widget to display some text in the preferred language

class SearchInstText extends CWidget
{
	public function run()
	{	
            $systemMessage = SystemMessage::model()->findByPk(11);
            
            if (isset(yii::app()->language))
                
                if (yii::app()->language === 'en_us')
                    $textVar = 'text';
                else
                    $textVar = 'text_fr_ca';
                    
            $this->render('searchInstTextView', array('systemMessage'=>$systemMessage[$textVar]));
	} 
}


and the view:

<?php 

echo $systemMessage;

?>



for the labels, hints and other little things, this is handled through Yii:t.
0

#3 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 09 October 2012 - 01:36 AM

View Postfilthy, on 08 October 2012 - 07:42 AM, said:

1. add language code table to your 'about' table.

id, description, language_id (fk)

2. populate with proper data

1 - this is a description - english
2 - ceci est une description - french
3 - bla bla bla - other language

3. in the app, you need to do a couple of things.

check for the language to be displayed and then fetch the description in the proper language.

To setup i18n, you need to do the following. I will show you what i did in my current project.

in main.php, set default language.

return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	// Application name
        'name'=>'PMMetriX',
        'sourceLanguage'=>'en_us',


in userIdentity, i set a session variable to the default language selected by the user

                             // set language code as per user preferrence - i.e. en_us, fr_ca,, etc. 
                                $languageCode = Language::model()->findByPk($user->language_id);
                                $app->user->setState('languageId', $languageCode->code);
      


i extended Controller so that everytime an action is requested, it populates Yii::app->user->languageId

    public function init()
    {
        if (isset(Yii::app()->user->languageId)) 
            return Yii::app()->language = Yii::app()->user->languageId;
    }  


i then created a widget to display some text in the preferred language

class SearchInstText extends CWidget
{
	public function run()
	{	
            $systemMessage = SystemMessage::model()->findByPk(11);
            
            if (isset(yii::app()->language))
                
                if (yii::app()->language === 'en_us')
                    $textVar = 'text';
                else
                    $textVar = 'text_fr_ca';
                    
            $this->render('searchInstTextView', array('systemMessage'=>$systemMessage[$textVar]));
	} 
}


and the view:

<?php 

echo $systemMessage;

?>



for the labels, hints and other little things, this is handled through Yii:t.

Thanks
Aneesh.
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