class I18n::Backend::LazyLoadable
def assert_file_named_correctly!(file, translations)
Checks if a filename is named in correspondence to the translations it loaded.
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
def available_locales
def available_locales if lazy_load? I18n.load_path.map { |path| LocaleExtractor.locale_from_path(path) }.uniq else super end end
def eager_load!
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
def filenames_for_current_locale
These files must start with the locale identifier (ie. "en", "pt-BR"),
Select all files from I18n load path that belong to current locale.
def filenames_for_current_locale I18n.load_path.flatten.select do |path| LocaleExtractor.locale_from_path(path) == I18n.locale end end
def init_translations
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
def initialize(lazy_load: false)
def initialize(lazy_load: false) @lazy_load = lazy_load end
def initialized?
def initialized? if lazy_load? initialized_locales[I18n.locale] else super end end
def initialized_locales
def initialized_locales @initialized_locales ||= Hash.new(false) end
def lazy_load?
def lazy_load? @lazy_load end
def load_translations_and_collect_file_errors(files)
translations as expected by the name. The method returns a list of
Loads each file supplied and asserts that the file only loads
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
def lookup(locale, key, scope = [], options = EMPTY_HASH)
def lookup(locale, key, scope = [], options = EMPTY_HASH) if lazy_load? I18n.with_locale(locale) do super end else super end end
def reload!
def reload! if lazy_load? @initialized_locales = nil @translations = nil else super end end