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.



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

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

Instance Method Details

#==(other) ⇒ Object

Is another loader is also load Rails translations.



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

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

#availableObject

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



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

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

#hashObject

Return hash for object and I18n.load_path.



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

def hash
  ::I18n.load_path.hash
end

#load(locale) ⇒ Object

Return Hash with translations for locale.



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

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

#reload!Object

Reload backend if I18n.load_path is changed.



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

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