Lets make one website Into All language IN YII style.
Pranavjeet Mishra
UI Architect crafting seamless experiences for the last 12 years, one pixel at a time. AI Expert in LangChain and RAG. Good Expertise in AWS BEDROCK , OPEN AI , ANTHROPIC CLAUDE.
First Extent the application Controller :
Never forget default controller , he is dad Ofcourse !!!
Extend it
class ApplicationController extends Controller{
public function init(){
//here to have to fill the cookies enabled user’s choice
if(isset(Yii::app()->request->cookies['language']->value))//If there is language defined in cookie, use it
Yii::app()->language = Yii::app()->request->cookies['language']->value;
else
Yii::app()->language = 'en';
//save user’s preferred language in cookie, and load it upon page load. specific code for generate Menu, Yii's default language
Yii::app()->language = 'en'; }
Yes we generated controller using Gii tool. This enables you to easily configure your application.set language default of your choice, and this applies to all other controllers which will extend ApplicationController. I had set it to English.
Buddy , I need a function ,tell me function name …. yes it is
Yii::t() function
function itself enables you internationalization
Yii::t('label','name')
Yii::t('errorMessages','@#$%')
Yii::t('successMessages','Good YOU FOUND it ')
Yii::t('notifications','You have {numberOfMessages} messages', array('numberOfMessages'=>$messages))
Now it time to make categories !
Don’t ask me why ! because you may need some notification of messages to users what he done , if they Know HINDI , show them in that too !!
Yii::t('label','name')
Yii::t('errorMessages','Oops! Not found')
Yii::t('successMessages','Found!')
Yii::t('notifications','You have {numberOfMessages} messages', array('numberOfMessages'=>$messages))
Label , error message , success message , notification , are categories . You can create as many you need !
Well if you working with module , then prefix the module name .
For example , for owner or site administrator .
Owner module , prefix “owner “
Yii::t('ownerModule.label',' name')
Use YIIc tool to generate translated files inside framework library ,
Go to linux or windows console and change directory of framework as follows
cd YOUR PATH TO YII FRAMEWORK FOLDER / framework/
open notepad++ type this code
<?php
/**
* This is the configuration for generating message translations
* for the Yii framework. It is used by the 'yiic message' command.
*/
return array(
//Folder that should be translated
'sourcePath'=>'FULL PATH TO YOUR APPLICATION PROTECTED FOLDER',
//Example: /srv/www/<wbr />htdocs/public/pranavapp/protected/
'messagePath'=>'FULL PATH TO YOUR MESSAGES FOLDER. HERE YOUR LANGUAGE FILES WILL APPEAR',
//Example: /srv/www/htdocs/public/pranavapp/protected/messages
'languages'=>array('de'),
'fileTypes'=>array('php'),
'overwrite'=>true,
'exclude'=>array(
'.svn',
'.gitignore',
'yiilite.php',
'yiit.php',
'/i18n/data',
'/messages',
'/vendors',
'/web/js',
),
);
Save as Script.php in framework folder.
Run the command to generate language files, where you can translate your website.
Type in console :
php yiic message script.php
Now , we have multiple modules to work with like login , message , transaction , cart, newsletter an all, just direct the module with the specifing ‘sourcePath‘ as path to your module files.
Ex:
Transaction module
'sourcePath'=>'/srv/www/htdocs/public/testapp/protected/modules/transaction/',
'messagePath'=>'/srv/www/htdocs/public/testapp/protected/modules/transaction/messages/',
VOILA !!!! Done , Cheers and enjoy website of your choice.