Class: R18n::Loader::Rails

Inherits:
Object
  • Object
show all
Includes:
YamlMethods
Defined in:
lib/r18n-rails-api/loader.rb

Overview

Loader for translations in Rails I18n format:

R18n::I18n.new('en', R18n::Loader::Rails.new)

It use Rails I18n backend to load translations. By default, simple backend will be used, by you can change it, if use extended backend (for example, with ActiveRecord storage):

R18n::I18n.new('en',
               R18n::Loader::Rails.new(I18n::Backend::ActiveRecord.new))

Instance Method Summary collapse

Constructor Details

#initialize(backend = ::I18n::Backend::Simple.new) ⇒ Rails

Create new loader for some backend from Rails I18n. Backend must have reload!, init_translations and translations methods.



40
41
42
43
# File 'lib/r18n-rails-api/loader.rb', line 40

def initialize(backend = ::I18n::Backend::Simple.new)
  @backend = backend
  detect_yaml_private_type
end

Instance Method Details

#==(loader) ⇒ Object

Is another loader is also load Rails translations.



73
74
75
# File 'lib/r18n-rails-api/loader.rb', line 73

def ==(loader)
  self.class == loader.class
end

#availableObject

Array of locales, which has translations in I18n.load_path.



46
47
48
49
# File 'lib/r18n-rails-api/loader.rb', line 46

def available
  reload!
  @translations.keys.map { |code| R18n.locale(code) }
end

#hashObject

Return hash for object and I18n.load_path.



68
69
70
# File 'lib/r18n-rails-api/loader.rb', line 68

def hash
  ::I18n.load_path.hash
end

#load(locale) ⇒ Object

Return Hash with translations for locale.



52
53
54
55
56
# File 'lib/r18n-rails-api/loader.rb', line 52

def load(locale)
  initialize_types
  reload!
  @translations[locale.code.downcase]
end

#reload!Object

Reload backend if I18n.load_path is changed.



59
60
61
62
63
64
65
# File 'lib/r18n-rails-api/loader.rb', line 59

def reload!
  return if @last_path == ::I18n.load_path
  @last_path = ::I18n.load_path.clone
  @backend.reload!
  @backend.send(:init_translations)
  @translations = transform @backend.send(:translations)
end