by

Hasibur

20 Mar 2015, 07:21 am

about the author

An expert in web development, he has an inclination towards photography as well. He loves playing cricket and football, though his pillar-like stature calls him for basketball too.

About Hasibur

Creating A Multi Language Website Using CakePHP

CakePHP

CakePHP is a robust open source PHP framework that works around the Model View Controller (MVC) framework. This framework is preferred mostly by beginners.

 

Being similar in its framework with that of Ruby on Rails, users who have already worked with the latter will find CakePHP’s use very similar. CakePHP provides users with some unique user experience that other frameworks can hardly match.

Also Read: WordPress Ecommerce Development With Best WordPress Plugins

The top advantages of CakePHP are:

Class Inheritance – CakePHP allows inheritance of classes. Though CakePHP is segmented into two folds, addition and extension is allowed in only a single one that is the AppController. All you have to do is add some extended logic to the AppController class. This allows you to make any form of changes within the classes.

Plugins and Extension – These form the most useful components. Once created, plugins and extension can later be reused in other projects as well. You can incorporate a few changes if you want as the basic code remains the same letting you customize different projects easily.

ORM – This stands for Object Relational Mapping. ORM is used mainly to translate incompatible type systems data into a more understandable programming language. This helps you prepare more understandable and related tables for each classes.

Apart from these, there are many other functionalities that give CakePHP an upper hand from other frameworks.

.CakePHP has certain sets of code already. Hence, a developer need not start from scratches every time he gets to develop a website. Its community of active developers helps you by providing you with application logic. This prevents developers from re-inventing the PHP codes while creating a new website reducing the time for completion.

CakePHP Framework

Catering to multiple languages in a website is a great way to reach a larger number of audiences. This increases your traffic as well as helps in internationalization of your website

If you build a PHP application using the CakePHP framework, it is easy to localize the application into multiple languages, provided you have proper translations for those languages. If you want to internationalize your application to a global market, it is important to localize it for each language and region you want to target.

We can set a language by default by using the configure statement in core.php under app/config folder

<pre>
Configure::write('Config.language', 'en');

You can look at the file lib/Cake/I18n/L10n.php, to know how to call each language.

You have to: create files and folder as follows:


/app/Locale/eng/LC_MESSAGES/default.po (English)

/app/Locale/fre/LC_MESSAGES/default.po (French)

In default.po files, you need to create pairs:

Where msgid is the ID within the __() function; msgstr is the localized translation that should appear as the output.

Our full English source PO file will look like this:


msgid "Hello"

msgstr " Translation"

msgid "world"

msgstr " Translation"

Changing language in real time

Lets say, you plan to make a website that will have a multilanguage option. Your pages display the option of letting users choose the language of their choice. You can have an array of languages option displayed in the header. When a user clicks on any of the languages, he is redirected to the same URL but in a different language.

How can you incorporate the same you say? Well, first of all, we will make the changes over the session in order to change the language in real time.

Set your selected language in session variable


$this->Session->write('Config.language', 'fre');

Read your session variable value in appcontroller beforeFilter function and set your default language.


class AppController extends Controller {

            public function beforeFilter() {

            Configure::write('Config.language', $this->Session->read('Config.language'));

            }

}

in your view file


&nbsp;

<h2><?php echo __(Hello); ?></h2>

by

Hasibur

20 Mar 2015, 07:21 am

about the author

An expert in web development, he has an inclination towards photography as well. He loves playing cricket and football, though his pillar-like stature calls him for basketball too.

About Hasibur
Comment with Facebook

trending

Featured Posts

Editors Choice

Reviews