Module: Einvoicing::I18n

Defined in:
lib/einvoicing/i18n.rb

Overview

Thin wrapper around ::I18n for gem-internal translations. Loads the gem's own locale files on setup; in Rails apps the engine already handles the load_path, so duplicates are skipped.

Constant Summary collapse

DEFAULT_LOCALE =
:en
LOCALES_PATH =
File.expand_path("../../config/locales", __dir__)

Class Method Summary collapse

Class Method Details

.setupObject



13
14
15
16
17
18
19
20
# File 'lib/einvoicing/i18n.rb', line 13

def self.setup
  locale_files = Dir[File.join(LOCALES_PATH, "*.yml")]
  new_files = locale_files - ::I18n.load_path
  return if new_files.empty?

  ::I18n.load_path += new_files
  ::I18n.backend.load_translations
end

.t(key, **options) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/einvoicing/i18n.rb', line 22

def self.t(key, **options)
  locale = options.delete(:locale) { ::I18n.locale }
  ::I18n.t("einvoicing.#{key}", locale: locale, **options)
rescue ::I18n::MissingTranslationData
  ::I18n.t("einvoicing.#{key}", locale: DEFAULT_LOCALE, **options)
rescue StandardError
  key.to_s
end