Hello Readers,
Hope you are doing good today.
Today we will discuss multilingual functionality on the website. The main question is that Why You Need A Multilingual Website For Your Business? Nowadays you see, times are changing with the growing number of people buying online from all over the world, there is a massive potential them in their native language. In this blog, you will learn how to create your website configuring.
Here we are using English and Hindi language.
First, you need to add below code in your index.ctp file.
<?php
echo $this->Html->link('English', array('action'=>'changeLanguage','eng'));
echo $this->Html->link('Hindi',array('action'=>'changeLanguage','hin'));
//To internationalize your code, all you need to do is to wrap strings in __() like so:
echo __('welcome to multilingual website');
?>
Now you need to set your default language in app/Config/core.php
Configure::write('Config.language', 'eng');
After configure default language in the core.php file now you need to create the pot file for insert string in the different language.
/app/Locale/eng/LC_MESSAGES/default.po (English)
/app/Locale/hin/LC_MESSAGES/default.po (Hindi)
Structure of .po file(Open default.po file and put your language translated text)
//Where msgid is the ID within the __() function; msgstr is the localized translation that should appear as the output.
msgid "welcome to multilingual website"
msgstr " "
Now go to your AppController and create the function which writes the language in session according to your choice.
Before create the function set the component in AppController like below.
var $components = array('Session', 'Cookie');
Create the function changeLanguage().
public function changeLanguage($lang){
if(!empty($lang)){
//echo $lang;die;
if($lang == 'eng'){
$this->Session->write('Config.language', 'eng');
}
else if($lang == 'hin'){
$this->Session->write('Config.language', 'hin');
}
$this->redirect($this->referer());
}
}
Put below code in your beforeFilter () function which read the selected language from the session.
public function beforeFilter() {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
I hope this will help you. Please feel free to give us your feedback in comments.
0 Comment(s)