class I18n::Backend::LazyLoadable

Public Class Methods

new(lazy_load: false) click to toggle source
# File lib/i18n/backend/lazy_loadable.rb, line 66
def initialize(lazy_load: false)
  @lazy_load = lazy_load
end

Public Instance Methods

available_locales() click to toggle source

Parse the load path and extract all locales.

Calls superclass method
# File lib/i18n/backend/lazy_loadable.rb, line 99
def available_locales
  if lazy_load?
    I18n.load_path.map { |path| LocaleExtractor.locale_from_path(path) }
  else
    super
  end
end
eager_load!() click to toggle source

Eager loading is not supported in the lazy context.

Calls superclass method
# File lib/i18n/backend/lazy_loadable.rb, line 90
def eager_load!
  if lazy_load?
    raise UnsupportedMethod.new(__method__, self.class, "Cannot eager load translations because backend was configured with lazy_load: true.")
  else
    super
  end
end
initialized?() click to toggle source

Returns whether the current locale is initialized.

Calls superclass method
# File lib/i18n/backend/lazy_loadable.rb, line 71
def initialized?
  if lazy_load?
    initialized_locales[I18n.locale]
  else
    super
  end
end
lookup(locale, key, scope = [], options = EMPTY_HASH) click to toggle source
Calls superclass method
# File lib/i18n/backend/lazy_loadable.rb, line 107
def lookup(locale, key, scope = [], options = EMPTY_HASH)
  if lazy_load?
    I18n.with_locale(locale) do
      super
    end
  else
    super
  end
end
reload!() click to toggle source

Clean up translations and uninitialize all locales.

Calls superclass method
# File lib/i18n/backend/lazy_loadable.rb, line 80
def reload!
  if lazy_load?
    @initialized_locales = nil
    @translations = nil
  else
    super
  end
end

Protected Instance Methods

init_translations() click to toggle source

Load translations from files that belong to the current locale.

# File lib/i18n/backend/lazy_loadable.rb, line 121
def init_translations
  file_errors = if lazy_load?
    initialized_locales[I18n.locale] = true
    load_translations_and_collect_file_errors(filenames_for_current_locale)
  else
    @initialized = true
    load_translations_and_collect_file_errors(I18n.load_path)
  end

  raise InvalidFilenames.new(file_errors) unless file_errors.empty?
end
initialized_locales() click to toggle source
# File lib/i18n/backend/lazy_loadable.rb, line 133
def initialized_locales
  @initialized_locales ||= Hash.new(false)
end

Private Instance Methods

assert_file_named_correctly!(file, translations) click to toggle source

Checks if a filename is named in correspondence to the translations it loaded. The locale extracted from the path must be the single locale loaded in the translations.

# File lib/i18n/backend/lazy_loadable.rb, line 175
def assert_file_named_correctly!(file, translations)
  loaded_locales = translations.keys.map(&:to_sym)
  expected_locale = LocaleExtractor.locale_from_path(file)
  unexpected_locales = loaded_locales.reject { |locale| locale == expected_locale }

  raise FilenameIncorrect.new(file, expected_locale, unexpected_locales) unless unexpected_locales.empty?
end
filenames_for_current_locale() click to toggle source

Select all files from I18n load path that belong to current locale. These files must start with the locale identifier (ie. “en”, “pt-BR”), followed by an “_” demarcation to separate proceeding text.

# File lib/i18n/backend/lazy_loadable.rb, line 167
def filenames_for_current_locale
  I18n.load_path.flatten.select do |path|
    LocaleExtractor.locale_from_path(path) == I18n.locale
  end
end
lazy_load?() click to toggle source
# File lib/i18n/backend/lazy_loadable.rb, line 139
def lazy_load?
  @lazy_load
end
load_translations_and_collect_file_errors(files) click to toggle source

Loads each file supplied and asserts that the file only loads translations as expected by the name. The method returns a list of errors corresponding to offending files.

# File lib/i18n/backend/lazy_loadable.rb, line 152
def load_translations_and_collect_file_errors(files)
  errors = []

  load_translations(files) do |file, loaded_translations|
    assert_file_named_correctly!(file, loaded_translations)
  rescue FilenameIncorrect => e
    errors << e
  end

  errors
end