Showing posts with label rubyonrails. Show all posts
Showing posts with label rubyonrails. Show all posts

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

Thursday, April 3, 2008

Installing Ruby On Rails on Ubuntu with rmagick

My laptop hard disk drive (it was a fujitsu drive on an HP Pavilion nobebook pc) was gone withing just two months of purchase. Later I got the replacement but, I had to spent another to prepare rails stack again :(
Anyway, here is the recipe for Ubuntu 7.10
$ sudo apt-get install ruby irb ri rdoc ruby1.8-dev libopenssl-ruby1.8 build-essential
$ wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
$ tar xzvf rubygems-1.0.1.tgz
$ cd rubygems-1.0.1
$ sudo ruby setup.rb
$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
$ sudo gem update --system
$ sudo gem install rails
$ sudo gem install mongrel

I couldn't install rmagick on the first place. Because building rmagick (v2.3.0) requires ImageMagick version 3.0 or greater but current Ubuntu repository provides only v2.6 (by the time I wrote this). So, I had to install from the source. Thanks to Emmannual for this suggestion.
Remove ImageMagick, if you have already installed
sudo apt-get remove imagemagick

Now download, compile and install from the ImageMagick source
$ tar xvvzf ImageMagick.tar.gz
$ cd ImageMagick
$ ./configure –prefix=/usr
$ make
$ sudo make install
$ sudo gem install rmagick

Or alternatively, you can download binary of ImageMagick-dev from debian repo

$ sudo apt-get install libmagick9-dev
$ sudo gem install rmagick

One more step. Install all necessary gems that are required by the project you are working. Like, our current rails project uses the gem 'memcache-client'. So, I ran the following
$ sudo apt-get install memcached
$ sudo gem install memcache-client

Monday, February 25, 2008

Rails Time and Database

Last few days I improved rails code related to time function in several places.

1.

Rails extended basic Time class to give output in several format. So that you can output in database datetime format by passing :db symbol as argument to Time's to_s() method. Previously I produced database format manually
Time.now.strftime("%Y-%m-%d %H:%M:%S").to_s
now, it simplifies to
Time.now.to_s(:db)
Detail documentation can be found here

2.
To parse database date to ruby Time, I used this.
Time.parse(token.created_at.to_s)
where created_at is DateTime type field in Token model

3.
To get back-and-forth between Unix Timestamp and ruby Time, you can simply use -
Time.now.to_i
and to read -
Time.at(unix_timestamp)