<commit_before>require "copycat/engine"

=begin
from i18n gem, lib/i18n/backend/simple.rb

  # Looks up a translation from the translations hash. Returns nil if
  # eiher key is nil, or locale, scope or key do not exist as a key in the
  # nested translations hash. Splits keys or scopes containing dots
  # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
  # <tt>%w(currency format)</tt>.
  def lookup(locale, key, scope = [], options = {})
    init_translations unless initialized?
    keys = I18n.normalize_keys(locale, key, scope, options[:separator])

    keys.inject(translations) do |result, _key|
      _key = _key.to_sym
      return nil unless result.is_a?(Hash) && result.has_key?(_key)
      result = result[_key]
      result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
      result
    end
  end

=end
module CopycatImplementation
  def lookup(locale, key, scope = [], options = {})
    cct = CopycatTranslation.find_by_key(key)
    return cct.value if cct
    value = super(locale, key, scope = [], options = {}) || key
    CopycatTranslation.create(key: key, value: value)
    value
  end
end

class I18n::Backend::Simple
  include CopycatImplementation
end
<commit_msg>deal with fact that Rails calls I18n.t when trying to run migration to create table<commit_after>require "copycat/engine"

=begin
from i18n gem, lib/i18n/backend/simple.rb

  # Looks up a translation from the translations hash. Returns nil if
  # eiher key is nil, or locale, scope or key do not exist as a key in the
  # nested translations hash. Splits keys or scopes containing dots
  # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
  # <tt>%w(currency format)</tt>.
  def lookup(locale, key, scope = [], options = {})
    init_translations unless initialized?
    keys = I18n.normalize_keys(locale, key, scope, options[:separator])

    keys.inject(translations) do |result, _key|
      _key = _key.to_sym
      return nil unless result.is_a?(Hash) && result.has_key?(_key)
      result = result[_key]
      result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
      result
    end
  end

=end
module CopycatImplementation
  def lookup(locale, key, scope = [], options = {})
    begin
      cct = CopycatTranslation.find_by_key(key)
    rescue ActiveRecord::StatementInvalid
      raise if CopycatTranslation.table_exists?
      #assert Rails is initializing for the purpose of running the copycat_translations migration
      super 
    else
      return cct.value if cct
      value = super(locale, key, scope = [], options = {}) || key
      CopycatTranslation.create(key: key, value: value)
      value
    end
  end
end

class I18n::Backend::Simple
  include CopycatImplementation
end

{'fraction_non_alphanumeric': 0.10085227272727272, 'fraction_numerical': 0.006392045454545455, 'mean_word_length': 3.5954323001631323, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5953772', 'n_tokens_mistral': 872, 'n_tokens_neox': 857, 'n_words': 285}