I18N For Database Data

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)

  1. populate with proper data

1 - this is a description - english

2 - ceci est une description - french

3 - bla bla bla - other language

  1. 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.