Getting Text and Currency Working for Multiple Language Support
In this conclusion to a four-part article series on adding support for multiple languages to a Ruby-on-Rails ecomerce application, you will learn how to localize numbers and currency, set character encoding, and more. This article is excerpted from chapter 10 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Getting Text and Currency Working for Multiple Language Support - Adding Unicode (UTF-8) Support (Page 2 of 5 )
As we mentioned earlier, George wants to enter the Chinese market sooner or later. In China, the official character set is simplified Chinese, which contains about 6000 different characters. Languages like Simplified Chinese that contain hundreds or thousands of characters do not fit into the standard used by most languages in Europe and North America (ISO-8859-1). Instead, these languages require that we change the application to support UTF-8.
UTF-8 can represent any universal character in the Unicode standard and is backward-compatible with ASCII. UTF-8 is a variable-length character encoding, where each character can be encoded using one to four bytes.
Character encoding is a cause of many problems. For Unicode, or any other character encoding, to work properly, all parts of the system must use the same encoding. In most systems, like Rails-based web applications, this includes the following parts:
The HTML page
The HTTP response headers
The database connection
Failing to use the correct character encoding in one or more subsystems usually means that Unicode characters are displayed as question marks or some other invalid character. For example, instead of seeing,
,you would see ??? or some other characters.
Note The Ruby language doesn't support Unicode out-of-the-box. For example, String#length and String#substr won't work correctly with Unicode strings. See wiki.rubyonrails.com/rails/ pages/HowToUseUnicodeStrings for more information about how to add Unicode support to Ruby.