Tuesday, November 25, 2008

Ruby on Rails plugin to localize numeric digits (translate to any language)

Probably somewhere in... is the first company to build Bangla web2.0 solutions using rails. The first problem we faced is the lack of support for Unicode in Ruby. But fortunately we rarely needed to use string funtions and thus avoid the consequence. Secondly, we needed a stable and efficient il8n library(plugin) for Ruby on Rails to translate the interface (we had planned to roll out with support for two language initially). The rescue came from globalize plugin. Its pretty old and no update for quite a while, yet this robust plugin suits the best to our need. The translation list is backed by database. So, you can easily create admin panel to manage translation list.

Then we felt the need for another feature; representing numeric digits to local language. And its better if that can work seamlessly with our existing code written using globalize plugin. The plugin globalize_numeric, with globalize, can translate your digits to any language (currently have Banga, Hindi, and Arabic) with minimal change in code that uses globalize for localization.

To add other language, add the language digits to NUMERIC_LOCALIZE_MAP in 'lib/core_ext.rb' file.

Plugin's github repository

Monday, November 24, 2008

Converting Bangla (Bengali) digits to ascii ones

ruby 1.8 doesn't support unicode. This creates problem when I need to parse Bangla digits in a text where each Bangla digit is represented as three bytes in utf-8.

I used a simple approach to satisfiy my need - 1. take the the last byte of each bangla digit, and 2. subtract by last byte of bangla digit '0'. Converting the whole number is even simpler. Here is my code -

  def self.translate_number_from_bangla(p_bn_number)
en_number = 0
length = p_bn_number.length
for i in 1..length/3 do
cur_digit = p_bn_number[3*i-1].to_i - 166
if cur_digit >= 0 && cur_digit <= 9
en_number *= 10
en_number += cur_digit
end
end
return en_number
end

Tips: Update your rubygems (with special case)

The most suggested updating technique is
$ sudo gem update --system


But this technique may not work with rubygems version 1.1 and 1.2. run
$gem -v

to know your rubygems version.

For those having these two versions, install rubygems-update gem and then run update_rubygems command (as sudo if necessary)

$ sudo gem install rubygems-update
$ sudo update_rubygems