Wednesday, September 9, 2009

Internationalizing Your Cakephp Application(support multiple languages cakephp)

For some developers, allowing a website to support multiple languages is essential. Luckily cakePHP 1.2 has the foundations available to make this possible.

I certainly hope after using this guide you can quickly and easily implement multiple languages in your cake app without needing to skip around the place. If I fall short of this and you have suggestions, leave a comment.

Once you complete this tutorial your site will be able to:
1. display multiple languages
2. allow users to switch languages
3. store language settings in cookies, so returning visitors don't need to re-select their preferred language
The sites that I build typically require 3 languages:
1. British English(eng)
2. Koren (kor)
3. Japanes (jap)
Step 1: Setup the directories and file for your messages
app/locale/eng/LC_MESSAGES/ default.po
app/locale/kor/LC_MESSAGES/ default.po
app/locale/jap/LC_MESSAGES/ default.po

Example Code of app/locale/eng/LC_MESSAGES/ default.op file.
msgid "home"
msgstr "Home"
msgid "about_us"
msgstr "About us"
msgid "membership_options"
msgstr "Membership Options"
msgid "our_lessons"
msgstr "Our lessons"
msgid "community"
msgstr "Community"
msgid "contact_us"
msgstr "Contact Us"
msgid "faqs"
msgstr "FAQ's"

Step 2: Write some strings to translate.(.ctp view file code sample)


Step 3: Change the default language
// config/bootstrap.php
define(DEFAULT_LANGUAGE, 'eng');
Step 4: Let users change the language
Component Class:
class P28nComponent extends Object {
var $components = array('Session', 'Cookie');

function startup() {
if (!$this->Session->check('Config.language')) {
$this->change(($this->Cookie->read('lang') ? $this->Cookie->read('lang') : DEFAULT_LANGUAGE));
}
}

function change($lang = null) {
if (!empty($lang)) {
$this->Session->write('Config.language', $lang);
$this->Cookie->write('lang', $lang, null, '+350 day');
}
}
}
?>
Controller Class:
class P28nController extends AppController {
var $name = 'P28n';
var $uses = null;
var $components = array('P28n');

function change($lang = null) {
$this->P28n->change($lang);

$this->redirect($this->referer(null, true));
}

function shuntRequest() {
$this->P28n->change($this->params['lang']);

$args = func_get_args();
$this->redirect("/" . implode("/", $args));
}
}
?>
Controller Class:
//app_controller.php
class AppController extends Controller {
var $components = array('P28n');
}
?>
The final piece of code, are some custom routes that need to be added to cake/app/config/routes.php

//route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));

//forgiving routes that allow users to change the lang of any page
Router::connect('/eng/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'eng'));
Router::connect('/jap/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'jap'));
Router::connect('/kor/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'kor'));
Step 6: Links to change language
image("lang-3.gif", array("alt" => "english",'url' =>_ROOT.'lang/kor')); ?>

image("lang-2.gif", array("alt" => "english",'url' =>_ROOT.'lang/jpn')); ?>

image("lang-1.gif", array("alt" => "english",'url' =>_ROOT.'lang/eng')); ?>

9 comments:

  1. HI, Thanks for articles. It is good. Pls replace _ROOT.'lang/jpn' to _ROOT.'lang/jap'. OK. Bye. This is my skype id: webdevvn.

    ReplyDelete
  2. Hi,
    Please help me for paypal integration in cakephp.
    mail me at naren2009@in.com.
    Thanks in advance.

    ReplyDelete
  3. Please visit on below mention URL for PAYPAL solution.
    http://bakery.cakephp.org/articles/view/paypal-direct-payment-api-component

    ReplyDelete
  4. Great! Thanks a lot for sharing this information. I am also working on PHP projects and got plenty of projects online. I hope to work with you in the future.

    Php Programmer London

    ReplyDelete
  5. hi anurag iam shishir from jaipur I read your multilingual web application suggestion but i am heard that for create multilingual web application we need some table eg i18n ,and doing work on cake console but iam not undrstood fully how to enter value in i18n table and how to retrieve if u have any solution about to console cake plz explain that how to develop dynamic multilingual web application .

    thanks

    ReplyDelete
  6. Hi shishir,

    we did not need any table eg i18n for multilingual web application.Please read this article it will help out on implementation.

    The mention code is developed and tested by me.If you not able to understand you can call me.

    Anurag
    9953799743

    ReplyDelete
  7. Hello Anurag ,
    i'M feeling this difficulty in cakephp project.
    Actually i made multilingual project in which when i'm choosing any language it is showing me this url : www.spybubble.com/home/index/language:en
    Where as i want www.spybubble.com/en/home/index
    Can i do this? I tried router url also but then it is showing error Page not found.
    Please help.
    Thanks in advance.

    ReplyDelete
  8. Hi all I am shahshi shukla and I am working on multilanguage pro.
    I found your article Sir, this one is enough for the site content not for the database content it is just previously difined content ... Please help for the data coming from the table

    ReplyDelete
  9. Hi shahshi,

    Multilanguage data management on DB side is dependence on your schema approach. You can do like below mention example .

    Default Lang Table
    id
    content

    Lang Table Content
    id
    lang_id
    content

    ReplyDelete