Note: If you are looking for a way to set page titles in CakePHP it is here
There are basic two ways to creating a multilingual app in traditional PHP, using a file with variables for each of the phrase which our Coppermine Picture Gallery uses and then there is the gettext series of functions in PHP, which while a very good option for desktop apps can be a pain for web apps because they are dependent on server as well as client LOCALE. CakePHP V1.2 is the framework which allows you to take the quantum leap of using gettext like functionality with minimum of hassles and at the same time address the common problems which a PHP programmer faces while creating multilingual apps
What I outline below is an example of translating a sentence in a view and some tips on working with .po files. The sentence which I want to make multilingual is “Hello Grandma!” starting with the view – you write
<?php __('Hello Grandma') ?>
Thats it! As far as your ‘view’ is concerned it is now multilingual ready, no I am not joking really, if you do not do anything else the ‘Hello Grandma’ will still render properly, do note that you are not required to write echo or print if you use the __() function without the second parameter.
OK, while making the view multilingual ready take less work than your regular echo(ing) making the translations work takes a bit more work. We now want ‘Hello Grandma’ to appear in French – ‘Bonjour grand-maman’.
Start by creating a file /app/locale/fre/LC_MESSAGES/default.po
Note here that the ISO 639-2 language codes are used to name the folder, and you have rightly guessed fre is the code for French, the default.po file is what will contain the translated text and is written in the following simple pattern[code] msgid "Hello Grandma"
msgstr "Bonjour grand-maman"
Some people prefer the msgid to be more varaiblish like ‘hello_grandma’ (with a corresponding change in the view) but I rather prefer to give the msgid in plain English, which also happens to be the default language for all of my development.
Now the only step that remains is setting the language to use. For this in your controller write
If you write the same key to session then the language code in session will take precedence – I use this trick to allow for user selectable languages.
Since we are now using gettext, if you are on a *nix development machine you get a number of useful CLI tools which ease the process of creating .po files the first among them is xgettext a utility to extract gettext strings from source and create a .po file. If your view files is called hello.ctp then from the commandline while in the same directory as your view file do
[code]xgettext -L php --keyword=__ hello.ctp[/code] this will result in a file called messages.po which will have all the strings which you need to translate as msgid, xgettext is very powerful and can work on entire folders with subfolders. See the xgettext man page for more information.
Two other useful utilities are msgcat and msgattr, msgcat allows you to combine several message catalogs and msgattrib allows for attribute matching and manipulation on message catalog.
An idea for combining several .po files into one without any duplication can be done with something like
[code] msgcat --use-first general.po [^g]*.po | msgattrib --no-fuzzy -o default.po[/code] as shown here
If you have any particular trick of your own I would love to hear from you….
Update: Running ./cake extract from the command line allows you to extract all the messages to be translated into a convenient default.pot file