2

I've got a Ruby app that is not using Rails, but needs localization. I've been using the I18n gem, which works well for dates, times, and strings. It doesn't seem to do numbers, though, unless I'm missing something.

I found the NumberHelper class in ActionView from Rails, and this question says how you can include it in a non-Rails app. However, making my app dependent on the whole activemodel framework seems like overkill just to get a few numeric localization routines.

Is there a better way to approach this? Should I be using one of the other Ruby localization libraries instead?

Community
  • 1
  • 1
Lynn
  • 1,103
  • 15
  • 29
  • Have you tried making the number a string? As in, use the value `"8"` instead of `8`? – Jeremy Rodi Oct 15 '12 at 03:03
  • @drderp: When I said localizing numbers, I meant localizing with the delimeter and separator - like in some languages 1,000.23 would be 1.000,23. – Lynn Oct 15 '12 at 03:05
  • I thought that was also in I18n, weird... – Jeremy Rodi Oct 15 '12 at 03:09
  • @drderp: Perhaps it is, but when I do I18n.localize(1000.23) I get an error that localize only works for dates and times. I didn't see any other method to do it - maybe I'm missing one? – Lynn Oct 15 '12 at 03:30

1 Answers1

1

I've used the gem R18n for that: https://github.com/ai/r18n

It can do the same stuff as I18n but is more flexible and can also localize numbers. It does not have dependencies of other gems.

Basic usage:

  • Add r18n-core to Gemfile
  • Initialize with `R18n.extension_places << 'path/to/yml/files'

And then either:

i18n = R18n::I18n.new('en')
i18n.t[:foo][:bar] # => "Translated text"
i18n.l(123.45) # => "123.45"

Or the more static version:

R18n.locale = 'en'
R18n.t[:foo][:bar]
R18n.l(123.45)

Helpers exists (in other gems) for rails and sinatra

Lasse Skindstad Ebert
  • 3,370
  • 2
  • 29
  • 28