PostMultilingual Website with CakePHP

We all know that developing a website in CakePHP is very easy and also fast. Here’s how to create a multilingual website fast.

First open app/config/bootstrap.php and set the languages you want available for your website:


Configure::write('Config.languages', array(
'ro' => array(
'language' => 'Romanian',
'locale' => 'rum',
'localeFallback' => 'rum',
'charset' => 'utf-8'
),
'en' => array(
'language' => 'English',
'locale' => 'eng',
'localeFallback' => 'eng',
'charset' => 'utf-8'
),
)
);

I chose english and romanian for this example.

Now open your app controller (app/app_controller.php – create one if you don’t have it) and put this function in it:


function setLanguage() {
if(!isset($this->params['lang'])) $this->params['lang'] = 'ro';
$lang = $this->params['lang'];
App::import('Core', 'i18n');
$I18n =& I18n::getInstance();
$I18n->l10n->get($lang);
foreach (Configure::read('Config.languages') as $lang => $locale) {
if($lang == $this->params['lang'])
$this->params['locale'] = $locale['locale'];
}
}

All you have to modify in this function is the first line (that sets the default language). Change the default language to your chosen one.

It’s now time to create a route depending on the language. Open app/config/routes.php and add this route to it:


Router::connect('/:lang/:controller/:action/*', array('lang' => 'ro'), array('lang' => 'ro|en'));

Of course that this can change depending on the languages you are using and their number. You’re a smart boy so you’ll figure it out.

Now all you have left is to create the language files. Go to app/locale and create 2 folders: rum and en. In each of these folders create another folder called LC_MESSAGES. In the LC_MESSAGES folders you will now store the language files. Language files can be divided so it’s easier for you to store the translations ( .po files ).

For example, you can create a login.po, register.po, account.po and default.po.

In the language files you have to set the message id and message string.

English

msgid "hello"
msgstr "Hello"

Romanian

msgid "hello"
msgstr "Buna ziua"

And now to echo these strings on your website you have to remember the string id and the name of the language file (.po) it is stored in. Example:


<?
__d('default', 'hello', true);
?>

This will take the string with the id “hello” from the default.po file and echo it. And true means that it will echo the string.
Good luck

I Disclose

Stay Connected

Subscribe to RSS Feed

Subscribe to RSS Feed

Follow me on Twitter

Follow me on Twitter

Subscribe via e-mail

Subscribe via e-mail

Comments 6 Responses to “Multilingual Website with CakePHP”

  1. Simple and direct. Nice article.

    The function __d() of Cake is for work with domains of translations (more complex than this article). To simplify, Cake created __() function. This assumes domain as ‘default’.

    __(‘hello’, true) === __d(‘default’, ‘hello’, true).

  2. Thank you for your comment Juan. I posted it like this so people who read this article understand how to use the __d() function and be more organized by using different .po files.

  3. Hi i tried this example,i created an po file in the three diffrent folders lets say english,french,german and the three po files related to them as well and i tried to use the function:__d(‘default’, ‘kaps’, true);

    it is only returning the string “kapil” only,even i write in the po file defualt:

    msgid “kaps”
    msgstr “kaps234″

    Please help me out where i am missing out.

  4. I don’t really get it kapil. How can it return the string kapil if you don’t have this string in your .po file.

    Search through your files for the string it returns (in your case “kapil”) and see what the problem is there.

  5. @Sava:
    This is for a predefined mannual translation. Say if i have small site, but lets say i have “browse, upload elements”, it automatically take the default browser language.

    Q. How to make browser based translation? (telling browser via cakephp, to render as romanian language or dutch or english etc, what ever, browser is supporting.)

    Thank you

    1. @Shamun,
      All you have to do is to take the browser’s language like this:

      $language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

      and $language will be en-us,en;q=0.5 in my case.
      You can also get an easier to use string with substr.


      $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

      And $language will be en.

      And now just tell cakephp to use the language files for $language.


Post your comment

Leave a Reply

ss_blog_claim=35efd881137b13aae56d7e24920f8a2b