Difference between #13 and #17 of
How to set up Unicode

Revision #17 has been created by resurtm on Mar 8, 2011, 3:29:50 PM with the memo:

Fixed broken link to the docs of CApplication → charset property.
« previous (#13) next (#21) »

Changes

Title unchanged

How to set up Unicode

Category changed

TutorialHow-tos

Yii version unchanged

Tags changed

i18n, unicode

Content changed

To fix issues with display of special language characters once and for all there's a solution: Uuse Unicode (UTF-8) everywhere. Other Unicode encodings exists, like UTF-16, but they are far less used on the web. If everything' is set up to
 
 use Unicode, you can use mostly every language in your application. There are several places that all may need some configuration tuning to use Unicode:> Info: Strictly speaking, *Unicode* is a *character set*.
 
> It lists and names characters from every main language around the world.
 
> *UTF-8* is an *encoding*.
 
> It defines a mapping between Unicode characters and a sequence of bytes.
 
> UTF-8 has a main advantage over other Unicode encodings : it is backward compatible with ASCII.
 
 
There are several places that all may need some configuration tuning to use Unicode.
 
 
## 0. Yii Application ##
 
 
By default, Yii applications already suppose the character set is UTF-8. See [CApplication::charset](http://www.yiiframework.com/doc/api/1.1/CApplication#charset-detail). This is used for encoding text in HTML pages, e.g. by [CHtml::encode()](http://www.yiiframework.com/doc/api/CHtml/#encode-detail)


## 1. PHP script files ##
[...]
## 2. Database tables ##

EYou need to set to UTF-8 the encoding of your connection to the SQL server. It's recommended to set up every table in your database needs to use UTF-8the same charset for its content. The
 
, but if it's not the case, the SQL server will convert the text on-the-fly. So **this step isn't mandatory, but it's highly recommended**.
 
 
The 
configuration for that might differ between database systems.

### MySQL
[...]
Be very careful when doing this conversion and make sure you save the file with the changed
SQL statement in UTF-8 and convert it if nec
cessary. If not performed carefully
you can easily end up with messed up encodings, e.g. having ISO-8859-1 encoded
characters in a table with utf8 `CHARSET`.
[...]
```


 
## 4. Webserver/HTTP-Header ##
 
 
We also need to let the browser know, that we use UTF-8 with our pages.
The connection encoding can also be set with a SQL command. In MySQL and SQLite:
 
~~~
 
[SQL]
 
-- Beware, it's utf8, not utf-8!
 
SET NAMES utf8 ;
 
~~~
 
Such a command can be put in the `initSQLs` attribute of the `db` component.
 
The `charset` attribute introduced above should be sufficient, though.
 
 
## 4. Webserver/HTTP-Header ##
 
 
We also need to let the browser know, that we use UTF-8 with our pages. There are 3 levels for this. By decreasing priority order:
 
 
* in PHP, with `header('Content-Type: text/html; charset=utf-8');`
 
* in the webserver (Apache, etc)
 
* in the HTML with `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />`
 
The best place to do this is in the header of an HTTP response. Configuring this varies between different server software. >Tip: If you use this approach, there's no need to add additional header information about encoding to your pages. Using the HTTP header is enoughYou just have to overwrite the HTTP header when your page is not in HTML or in UTF-8, like `header('Content-Type: text/plain; charset=iso-8859-1');`.

### Apache

You can configure UTF-8 charset either in a `VirtualHost` section of your server
configuration or by adding this line into a `.htaccess` file in your `DocumentRoot`:
[...]
19 0
14 followers
Viewed: 135 813 times
Version: 1.1
Category: How-tos
Tags: i18n, unicode
Written by: Mike
Last updated by: Roman Solomatin
Created on: Feb 21, 2009
Last updated: 10 years ago
Update Article

Revisions

View all history