Recent comments

In Benchmarking requestAction
David wrote: Why you are not counting database connection impact? Is it really so unimportant?
In Nate Abele - PHP is dying
Victor wrote: When you say the following: "[PHP] is just too simple. It requires only a few lines to generate...
In Felix Geisendörfer - Git and CakePHP
Josh wrote: I'd really like to figure out a way to clone the official git repo and merge it with my own in a...
In Martin Radosta - Record level security based on SQL
markstory wrote: Well the article was about a talk that Martin Radosta gave. I have not written this behavior. ...
In Martin Radosta - Record level security based on SQL
skitle wrote: Was curious if the files for this are available? I have searched through the SF site, and have...
RSS Feed for comments

Mariano Iglesias - Internationalization with CakePHP

Written on Thu, Dec 4th 2008, 13:30 in ,

Why internationalize? You can attract a larger market by making content available in additional languages. When offering international content, you need to translate both the fixed and dynamic content in the database. In CakePHP you can facilitate translation with __() and __n(). In addition content stored in the database can be translated with TranslateBehavior. Multibyte characters exist in many languages, characters outside the traditional latin character set are represented with multibyte characters. This allows for the creation of additional characters and idiomatic expresssion. There is a PHP extentsion for using multibyte strings, it provides the mb_ functions. CakePHP also provides a MultiByte class which provides all the mb_ functions for PHP4 or PHP5 installations lacking them.

Using translation in CakePHP

By using __() we can create translatable strings in our applications. You can use placeholders as well in your translation strings. It is important to use place holders as parameters such as a name can move around in a sentance based on the language. Once you have added all the __() calls, you can use cake i18n extract -output app/locale. The generated file will be placed in app/locale/default.pot

Mariano then gave an example and quick walkthrough of translation and i18n. He added translated elements to the views. Then extracted the strings with the shell and created translated values with poEdit.

Translating database content

Translate behavior helps to translate any database content. All translations in CakePHP are stored in one table. Translate behavior takes a number of fields when included on a model. This indicates which fields are going to be translated. Translated fields are not needed on the table schema. The translate behavior also allows you to specify a number of languages and language specific values or you can simply add one language. Later on another language can be added. The rest of Translate Behavior works seamlessly on find() and save().

When using elements with caching. You can use a key parameter when calling element() to ensure that different languages are cached separately.

Mariano's talk brought the third day of CakeFest to an end. This has been an excellent conference so far and the last day promises to be great as well.

Back to Mark's articles

Comments:

Add comment
  • Nice Mark, this page is the fifth listed by Google for the search term "using __n() in cakephp" :)

    Reply | OpenID Provided | posted on 18/2/09

openID

What is OpenID?

OpenID is a new open standard that lets you sign in to web sites with a single URL that you own. This URL can be your homepage or blog, or it can be provided to you by a web site you use. In either case, you only have to sign in once to your OpenID provider and so you only need to maintain a single password.Learn more.

How is CakeDC using OpenID?

You can use your OpenID identity when posting comments on the site. When you see a form field with OpenID logo entering your OpenID identity is sufficient to allow your post. We also accept Google or Yahoo! identities. Simply use either "google.com" or "yahoo.com" and our OpenID library will locate your information from the appropriate source.