I18n without Rails?
Asked Answered
C

3

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.

Catechumen answered 16/10, 2012 at 14:44 Comment(0)
R
17

You already set the search path for the language definitions with I18n.load_path.

It seems, this is enough when using rails. Without rails, you must also load the language definitions with I18n.backend.load_translations.

In summary, you need two steps:

I18n.load_path = Dir['*.yml']
I18n.backend.load_translations

The dictionaries are defined with language key, e.g. like:

en:
  hello: "Hello world"

If you prefer to define your en.yml without language key, you may load them via

I18n.backend.store_translations(:en , YAML.load(File.read('en.yml')))

(You may also use a here-document or direct a ruby-hash).

Ruelas answered 2/1, 2013 at 21:15 Comment(0)
C
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.

Compelling answered 17/10, 2012 at 9:38 Comment(0)
C
0

You'll need to install rails-i18n gem just to get localization data.

With this gem install, one can e.g. print month names in sk localization with:

require 'rails-i18n'
I18n.load_path += $LOADED_FEATURES
                    .select  {|f| "rails-i18n.rb".in? f }
                    .collect {|f| f.sub('lib/rails-i18n.rb', 'rails/locale/sk.yml') }
I18n.locale = :sk
puts I18n.t('date.month_names').compact

This yields:

 Január
 Február
 Marec
 Apríl
 Máj
 Jún
 Júl
 August
 September
 Október
 November
 December
Chatelaine answered 12/6, 2018 at 20:3 Comment(2)
The question was about using i18n without Rails. So the i18n-rails gem would be the wrong solution.Floorboard
perfect answer, this was what I needed. rails-i18n can be used just to fetch the localisation data without having to use rails itself. Thanks, upvotedWieche

© 2022 - 2024 — McMap. All rights reserved.