1. Introduction
CakePHP’s localization system maybe good & performant, but it is also abit hard to implement/adjust and maintain. Most of the time, us developers will want to have a simple system to change the languages used, to change the words for each language and to debug the localization system. In the following minutes, we will [...]
1. Introduction
CakePHP’s localization system maybe good & performant, but it is also abit hard to implement/adjust and maintain. Most of the time, us developers will want to have a simple system to change the languages used, to change the words for each language and to debug the localization system. In the following minutes, we will build a module that will allow us to debug each translation from our application, have all of the sentences/words under the LOCALE folder, in files named as en_locale.php (can be however you want it but we will use this convention). Each file will contain an array with words/sentences where each pair of key/value represents the use word- translated word .
2. Preparing the envoirment
To begin our tutorial, we will need to install a new cakephp 1.2 envoirment or have an application installed and running.
We will use the following setup (customize at will ofcourse) – contents of each file will be below:
- We will use the Books Controller (/app/controllers/books_controller.php)
- We will use our Books Model (/app/models/book.php)
- Our view will be named index (/app/views/books/index.ctp)
- We will want a file named en_locale.php in our locale folder (/app/locale/)
- Make sure we have the file app_controller.php in your /app/
3. The code
-
Books controller
Explanation:
The Books controller only contains our action index that we will use to view our results.
-
Book model
The book model will only serve us not to use a database table (since our example won’t need such a thing)
-
Books index view
Here we see the actual function which we call to transform our text into the required language. If the expression does not exist in the language file or if the language file does not exist, in debug mode it will render the text as it is but in a red color folowed by the error in paranthese. Test the code to see exactly what this means.
-
Locale file (en_locale.php).
"Hello world"
);
?>
The locale file will be loaded based on your settings (i will explain this below) and will render the word based on the key. The convention is to use only lower case letters and spaces to be underscores.
- Application controller (app_controller.php)
0) {
echo '' . $msg . ' (Locale element does not exist in '.$lang.'_locale.php)';
} else {
echo $msg;
}
}
} else {
if (Configure::read() > 0) {
echo '' . $msg . ' (Locale file does not exist in app/locale/ - '.$lang.'_locale.php)';
} else {
echo $msg;
}
}
}
//end of custom locale function
Class AppController extends Controller {
//your app controller logic here
}
Here resides our function that will make the tutorial come to life. Here is a quick view of the function:
- It receives the $msg parameter which contains the message to transform, the $lang parameter which can overwrite the user’s language (for example you can call the function for FR specially) and the $mode parameter which stores how to get the language parameter.
- It checks to see if the language parameter has been sent. If it wasn’t sent then check , based on $mode parameter, gets the language code to use based on :
- Configure file (you can have an Configure::write(“language”,”EN”); in your core.php)
- User session, this means that previously you set-up the session parameter with your LANGUAGE_KEY (you can define this above the function as in the example)
- User cookie, which assumes that you setup the cookie for the user with your LANGUAGE_KEY
- The language parameter, by convention should be sent with capitalization letters (as in “EN” or “FR”) but the system will automatically change it to lower (so your files should be like “en_locale.php”).
- Once the $lang parameter has been setup, the function tries to see if the file exists. If it doesn’t then it will simply thrown an error along side the $msg sent which says that the file could not be found. If debug mode is OFF then it will just output the $msg variable.
- If the locale file exists, it loads it and checks to see if the element $msg exists in the array. If it doesn’t then it acts the same as above with the only mentionable change that it tells the user that the element is not present.
- If everything is ok then it prints on the screen the value from the array which contains the $msg key.
That was about it! Hope you liked it!
More will come!





