Alex Rivera | Logout

I18n without Rails?

Asked 2012-10-16T14:44:17.853
9

just having troubles to make I18n to work without Rails environment:


irb> require 'i18n'
=> true
irb> I18n.load_path=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml']
=> ["/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml"]
irb> I18n.load_path+=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/sk.yml']
=> ["/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml", "/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/sk.yml"]
irb> I18n.locale=:sk
=> :sk
irb> I18n.default_locale=:sk
=> :sk
irb> I18n.l Time.now
I18n::MissingTranslationData: translation missing:
sk.time.formats.default
  from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:289:in
`handle_exception'
  from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:159:in
`translate'
  from
/usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n/backend/base.rb:55:in
`localize'
  from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:236:in
`localize'
  from (irb):11
  from /usr/bin/irb:12:in `<main>'
irb>

What am I doing wrong ? The sk.yml DOES contain sk.time.formats.default element !!

In addition what's the I18n's default load_path(s) so I won't be bothered to supply full paths to every translation YAML/Ruby file ?

Thanks.

Edit
Report

1 Answer

3

It seems like your load_path is not being set correctly.

Try including the whole directory and if it's successful, you should see your :sk and :en files by calling I18n.load_path.

I18n.load_path = Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/*yml']

Setting the files paths directly can be a bit confusing since I18n won't raise an error if the file doesn't exist.

As a side note, I'd advise against including translations from the rails-i18n gem as the path may be different from one machine to another with different ruby versions etc.. a file local to the project would be better.

answered 2012-10-17T09:38:29.247

Your Answer