Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

248 | Chapter 8: i18n and L10n


The ActiveRecord PostgreSQL adapter also respects theencodingoption to control
client encoding, so remember to set it to UTF-8:


production:
adapter: postgresql
(...)
encoding: UTF-8

Serving UTF-8


Properly serving UTF-8 is a matter of telling the browser that you are using UTF-8.
This is done in two ways:


HTTPContent-type header with a charset parameter
This is the preferred way to set the encoding. The server should be configured to
spit out a header like:
Content-Type: text/html; charset=UTF-8
Rails takes care of this for us. As of Rails 1.2, the encoding automatically
defaults to UTF-8.


HTML tag
This method is often used by those who are not able to change their server’s con-
figuration to add a proper header. Thetag takes the place of the HTTP
header. Put this inside of thetag on your layouts for the same effect as the
header specified previously:



When used by itself, setting atag is less than ideal. This is because once a
browser reaches this tag, it must re-parse the document from the beginning if its
initial assumption about the encoding was incorrect. (This method works
because the characters likely to be used in an HTML document before the
tag have the same representation in all of the common encodings—they are the
low ASCII characters.)
However,tags are helpful when used in conjunction with proper server
headers. They allow the browser to determine the proper encoding even if the
file is saved locally (thus removing the header information).

Note that, in theContent-typeheader, the name “charset” is misleading, as this para-
meter really specifies the encoding.


Character Set Conversion


You must consider the issue of data you receive from external sources in non-UTF-8
encodings. If you serve HTML in UTF-8, the data you receive through form posts
will be UTF-8. But there are other external sources as well:

Free download pdf