Computer and Internet » Programming » Dojo Toolkit
Right Character Encoding with CakePHP and Dojo Toolkit
To get a system use the right character encoding standard can be really painful. There are many
places to make little errors. These little errors can make really big problems and are very hard to find!
There are some steps you have to check at each web application to be sure that the end user sees and
uses the right character encoding. I will explain these steps at server side with the php framework CakePHP
and at client side with the Javascript Framework Dojo Toolkit. At this document I use the encoding standard
UTF-8 because then you don't have any problems with choosing the right encoding.
Server Side
MySQL Backend
You can set the encoding of your database server in the configuration files. But when you use
your web application on a third party server where you don't know whose encoding is used you have
to define the right character encoding. At MySQL you can make this with one SQL statement.
set names 'utf8';
You have to make this query before you read or write any data at each new connection. When you use CakePHP
you have to extend your AppModel class. It's not possible to make the query only one time per connection
without touching the core code. But we really don't want to do that so we have to make this query for each
model used in your controller. Make a file named "app_model.php" and save it right into your "/app"
directory. Please implement this code:
class AppModel extends Model
{
function __construct($id=false, $table=null, $ds=null)
{
parent::__construct($id, $table, $ds);
$this->execute( "SET NAMES 'UTF8'" );
}
}
Client Side
Meta definition
First you should tell the browser which character encoding to use. There are some default values at client
side but you can't rely on these settings! With CakePHP you can use the HTML Helper in your layout to set
the Encoding
Please insert this line in your "head" area:
< ? echo $html->charset('UTF-8'); ? >
Form settings
A big problem is when you set the right character encoding but you send your data with another encoding
to the server for processing. You can define the encoding used for form processing by setting the
"accept-charset" attribute.
<form method="post" action="test.php" accept-charset="UTF-8">
Dojo.io.bind with Forms
When the form data is send with dojo.io.bind we have to set the encoding explicitly. There is only one
definition you have to implement to set the encoding application wide. We define it in the djConfig var.
var djConfig = {isDebug: true,
debugContainerId: "dojoDebug",
bindEncoding: "UTF-8"};
After that the whole dojo toolkit uses this settings.
Conclusion
When you implement this steps I think you have a very stable system and don't have to worry about false
encodings. When you know some other settings (especially for the php interpreter) please contact me and I will
publish your tips.
Tags: -
Related entries:
- Dojo Widget Cheat Sheet
- Populate SELECT Tag with Dojo and CakePHP
- How can i set Aptana to correctly highlight my .thtml templates from CakePHP
- Why does concat not use the default character encoding?
- I am using Infosoftglobal FusionCharts. When I want to update the Chart with javascript setXMLData nothing is happen in Internet Explorer. Everything works fine in Firefox!
Last update: 2006-09-18 12:43
Author: Christian Trummer
Revision: 1.1
You cannot comment on this entry