Home arrow Ruby-on-Rails arrow Page 5 - Getting Text and Currency Working for Multiple Language Support

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
July 02, 2010
  1. · Getting Text and Currency Working for Multiple Language Support
  2. · Adding Unicode (UTF-8) Support
  3. · Setting Character Encoding in HTML
  4. · Setting Character Encoding for the HTTP Response
  5. · Changing the Database to Use UTF-8

print this article

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.

mysql> show variables like 'character\_set\_%';
| Variable_name            | Value  |
| character_set_client     | latin1 |
| character_set_connection | latin1 |
| character_set_database   | latin1 |
| character_set_filesystem | binary |
| character_set_results    | latin1 |
| character_set_server     | latin1 |
| character_set_system     | utf8   |
7 rows in set (0.00 sec)

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:

adapter: mysql
database: emporium_development
username: emporium
password: hacked
encoding: utf8
adapter: mysql
database: emporium_test
username: emporium
password: hacked
encoding: utf8

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.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials