Rails is great when you want to start quickly and 10 minutes later have your first web application up and running. This is because many things that are needed are pre-configured. This is true but unfortunately if you live in a part of the world where everyone speaks a weird sounding language that has characters that are not standard, you have to use Unicode.
I live in this part of the world where everyone speaks Greek. Somehow we manage to communicate, but that’s another story.
I want to share with you what I found the hard way. How to setup your Rails application to handle Greek language without any problems:
- I use MySQL and set everything up from the database to the table, to use DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
- In your environment.rb file, there is a section # Include your application configuration below, where you should put the following two lines:
- $KCODE = ‘UTF8’
- require ‘jcode’
- In your database.yml, in your various configurations you add the following line:
- encoding: utf8
- IMPORTANT: in order to be able to write Greek characters in your rhtml or rb files you should save them in UTF8. Please be careful to use an editor that allows you to save in UTF8 without using a BOM. If this is not the case, Ruby interpreter will complain and you are going to get strange errors.
Disclaimer: All the tips above were/are essential if you are using Ruby 1.8.6 + Rails 1.2.3. I don’t know if installing the latest and greatest makes the tips redundant. If anyone knows, please respond with a comment here.