Saturday, October 06, 2007

distance_of_time_in_words in Turkish

In certain websites, you sometimes see notes like 'this comment was added 2 hours ago' .
If you use RoR (Ruby on Rails for the uninitiated), distance_of_time_in_words method in DateHelper accomplishes that.
What if your site is in another language, how do you manage to translate these messages? It's pretty simple actually...
For example for a Turkish implementation, simply append the code below to the end of your
application.rb file.

module ActionView::Helpers::DateHelper
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round

case distance_in_minutes
when 0..1
return (distance_in_minutes==0) ? 'bir dakika kadar önce' : '1 dakika önce' unless include_seconds
case distance_in_seconds
when 0..5 then '5 saniye kadar önce'
when 6..10 then '10 saniye kadar önce'
when 11..20 then '20 saniye kadar önce'
when 21..40 then 'yarım dakika kadar önce'
when 41..59 then 'bir dakika kadar önce'
else '1 dakika önce'

when 2..45 then "#{distance_in_minutes} dakika önce"
when 46..90 then '1 saat kadar önce'
when 90..1440 then "#{(distance_in_minutes.to_f / 60.0).round} saat kadar önce"
when 1441..2880 then '1 gün önce'
else "#{(distance_in_minutes / 1440).round} gün önce"

Sunday, July 29, 2007

Java or Rails?

It was almost one year ago. I was trying to decide which mvc framework to use for my new web project. I had implemented my last project in Java ( And I was a big Java fan; in some ways I still am.
But on the other hand, I was growing frustrated with certain aspects of the Java community.
Thinking back, I now realize what pushed me away from Java was Hibernate. Yes that great ORM tool. You know the old adage: 'you concentrate on your business logic, let us take care of the low level stuff'. With hibernate, this became: 'you can't concentrate on your business logic, you first have to figure out how to use the new version of hibernate'.
And it was the same with tools like Maven. Your old project is with Maven 1.0.x; by the time you start the new project, Maven 2.0.x is out...Well you first have to figure out how to use that one.
It's a project build tool for god's sake, why do I have to keep figuring out how to use it?

Internet is a great information resource obviously. But it may also become your biggest obstacle if you are trying to make a decision. Try buying a new digital camera basing your decision on user reviews. You'll probably go mad. What you need is a review written by your clone. Or if you believe in parallel universes, you have to figure out how to tap into that.
How can I decide which camera to buy by reading reviews written by a professional photographer? Isn't his insight useful? Well not to me necessarily.

I kept reading about Java frameworks first. I tried JFC, Tapestry, Wicket, Stripes...
Stripes was the one that won my heart. But I was still not at ease with my decision. Then I took a turn...And decided to give Ruby on Rails a run...

And that turned out to be one of the best decisions I've ever made.

ROR is a pleasure to write a web application in. I'm sure you've already read about 10x productivity gains...And you are curious whether that's true. Trust me it is...At least 10x.

'But what about all I've read about ROR being slow' you might ask. I personally haven't had any problems with that either. And we are running a high traffic site with a lot of back-end processes.

Ruby is a great programming language. Programming is more fun with a functional programming language. You feel more like god (you know what I'm talking about).

And if you don't want to leave your favourite java libraries behind, then there is JRuby for you.

If you are at the crossroads as I was a year ago, and you are trying to decide what to use; I say use RoR. You'll thank yourself. You'll love yourself. You'll become a better programmer.