class I18n::Backend::LazyLoadable
Public Class Methods
Source
# File lib/i18n/backend/lazy_loadable.rb, line 66 def initialize(lazy_load: false) @lazy_load = lazy_load end
Public Instance Methods
Source
# 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) }.uniq else super end end
Parse the load path and extract all locales.
Source
# 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
Eager loading is not supported in the lazy context.
Source
# File lib/i18n/backend/lazy_loadable.rb, line 71 def initialized? if lazy_load? initialized_locales[I18n.locale] else super end end
Returns whether the current locale is initialized.
Source
# 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
Source
# File lib/i18n/backend/lazy_loadable.rb, line 80 def reload! if lazy_load? @initialized_locales = nil @translations = nil else super end end
Clean up translations and uninitialize all locales.
Protected Instance Methods
Source
# 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
Load translations from files that belong to the current locale.
Source
# File lib/i18n/backend/lazy_loadable.rb, line 133 def initialized_locales @initialized_locales ||= Hash.new(false) end
Private Instance Methods
Source
# 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
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.
Source
# 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
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.
Source
# File lib/i18n/backend/lazy_loadable.rb, line 139 def lazy_load? @lazy_load end
Source
# 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
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.