Module: Phrasing::Implementation

Included in:
I18n::Backend::Simple
Defined in:
lib/phrasing/implementation.rb

Instance Method Summary collapse

Instance Method Details

#lookup(locale, key, scope = [], options = {}) ⇒ Object

this method overrides part of the i18n gem, lib/i18n/backend/simple.rb



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/phrasing/implementation.rb', line 4

def lookup(locale, key, scope = [], options = {})
  return super unless ActiveRecord::Base.connected? && PhrasingPhrase.table_exists?

  scoped_key = I18n.normalize_keys(nil, key, scope, options[:separator]).join(".")

  cct = PhrasingPhrase.where(locale: locale.to_s, key: scoped_key).first
  return cct.value if cct

  value = super(locale, key, scope, options)
  if value.is_a?(String) || value.nil?
    phrasing_phrase = PhrasingPhrase.new
    phrasing_phrase.locale = locale.to_s
    phrasing_phrase.key = scoped_key
    phrasing_phrase.value = value
    phrasing_phrase.save
  end
  value
end