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 - Changing the Database to Use UTF-8 (Page 5 of 5 )
The default character encoding in your database is probably currently latin1 (the default for MySQL). It might be different depending on how you installed MySQL and your system settings. You can check the encoding by opening a mysql client and typing in the following command:
show variables like 'character\_set\_%';
This will show you the character set used by different parts of the system. If you see that the character encoding for the database is not UTF-8, as in the following sample output, you need to fix it by changing the database to UTF-8.
$ mysql -uemporium -phacked emporium_development
--------------------------------------------Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 5.0.20-community
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
If you try to save Unicode text, it will end up being converted to question marks. To use a different character encoding, we need to specify the encoding when we create the database tables. Since we have been using migrations to create the tables, we must modify all of the ActiveRecord migrations. This is done by adding the options parameter to the create_table method:
create_table :publishers, :options => 'default charset=utf8' do |table|
MySQL uses utf8 as the encoding name, instead of the more common format utf-8 .
After changing the character encoding, re-create the database from scratch with the migrations. And remember to create the globalize tables again with rake globalize:setup .
Tip You can also specify the default character encoding used by MySQL at startup by using the --character-set-server parameter. After changing the character encoding, all new tables will use the default character encoding, so the migrations don’t need to be changed.
The last part of the system that you should change to use the correct encoding is the database connection. Change config/database.yml as shown here:
Restart WEBrick so that it picks up the changes to the configuration. Remember to add the same setting for the production database connection.
Now access the translation view one last time. Add some Chinese to the Text field, and the translation “Next page” for it, and then click the Add translation button. The Chinese characters should show up correctly in the list.
That’s it! George’s application is now Globalized, internationalized, and ready for world domination.
In this chapter, we introduced the Globalize plugin, which can be used to localize an application for multiple locales and languages. We showed you how to install the Globalize plugin and how to create a translation view for easy translation of content. We also showed you how to localize dates, numbers, and text, and how to add Unicode support to your applications.
In the next chapter, we will introduce you to Selenium, Selenium on Rails, and acceptance tests.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.