<commit_before>raise 'I18n version 0.6.0 is needed for a good string interpolation' unless I18n::VERSION >= '0.6.0'

module TermsHelper

  I18nSeparator = '.'

  Terms = [:profile, :supplier]
  Auxiliars = [
    nil,
    #
    :it, :one,
    :to_it,
    #
    :article, :undefined_article,
    :in, :which, :this, :your,
    :at, :at_article,
    :to, :to_article,
    :on, :on_your,
    :by, :by_article,
    :from, :from_article, :from_this, :from_which, :from_which_article, :by_your,
    # adjectives
    :none, :own, :new,
    :by_own, :new_undefined_article
  ]
  Variations = [nil, :singular, :plural]
  Transformations = [:capitalize]

  # FORMAT: terms.term.auxiliar.variation.transformation
  Keys = Terms.map do |term|
    Auxiliars.map do |auxiliar|
      Variations.map do |variation|
        [term, auxiliar, variation].compact.join I18nSeparator
      end
    end
  end.flatten

  @translations = {}
  def self.translations
    @translations
  end

  @cache = {}
  def self.cache
    @cache
  end

  protected

  def self.included base
    base.send :include, I18nAutoScope
    base.alias_method_chain :translate, :terms
    base.alias_method_chain :translate, :terms_cache
    base.send :alias_method, :t, :translate
  end

  def translate_with_terms key, options = {}
    translation = translate_without_terms key, options
    translation % translated_terms
  end

  def translate_with_terms_cache key, options = {}
    cache = (TermsHelper.cache[I18n.locale] ||= {})
    cache = (cache[i18n_scope] ||= {})

    hit = cache[key]
    return hit if hit.present?

    cache[key] = translate_without_terms_cache key, options
  end

  private

  def translated_terms keys = Keys, translations = TermsHelper.translations, transformations = Transformations, sep = I18nSeparator
    translated_terms = (translations[I18n.locale] ||= {})
    translated_terms = (translated_terms[i18n_scope] ||= {})

    return translated_terms if translated_terms.present?

    keys.each do |key|
      translation = self.translate_with_auto_scope "terms#{sep}#{key}", :raise => true rescue nil
      next unless translation.is_a? String

      translated_terms["terms#{sep}#{key}"] = translation<commit_msg>Don't cache if options is present<commit_after>raise 'I18n version 0.6.0 is needed for a good string interpolation' unless I18n::VERSION >= '0.6.0'

module TermsHelper

  I18nSeparator = '.'

  Terms = [:profile, :supplier]
  Auxiliars = [
    nil,
    #
    :it, :one,
    :to_it,
    #
    :article, :undefined_article,
    :in, :which, :this, :your,
    :at, :at_article,
    :to, :to_article,
    :on, :on_your,
    :by, :by_article,
    :from, :from_article, :from_this, :from_which, :from_which_article, :by_your,
    # adjectives
    :none, :own, :new,
    :by_own, :new_undefined_article
  ]
  Variations = [nil, :singular, :plural]
  Transformations = [:capitalize]

  # FORMAT: terms.term.auxiliar.variation.transformation
  Keys = Terms.map do |term|
    Auxiliars.map do |auxiliar|
      Variations.map do |variation|
        [term, auxiliar, variation].compact.join I18nSeparator
      end
    end
  end.flatten

  @translations = {}
  def self.translations
    @translations
  end

  @cache = {}
  def self.cache
    @cache
  end

  protected

  def self.included base
    base.send :include, I18nAutoScope
    base.alias_method_chain :translate, :terms_cache
    base.alias_method_chain :translate, :terms
    base.send :alias_method, :t, :translate
  end

  def translate_with_terms key, options = {}
    translation = translate_without_terms key, options
    translation % translated_terms
  end

  def translate_with_terms_cache key, options = {}
    # we don't support cache with the options
    return translate_without_terms_cache key, options if options.present?

    cache = (TermsHelper.cache[I18n.locale] ||= {})
    cache = (cache[i18n_scope] ||= {})

    hit = cache[key]
    return hit if hit.present?

    cache[key] = translate_without_terms_cache key, options
  end

  private

  def translated_terms keys = Keys, translations = TermsHelper.translations, transformations = Transformations, sep = I18nSeparator
    translated_terms = (translations[I18n.locale] ||= {})
    translated_terms = (translated_terms[i18n_scope] ||= {})

    return translated_terms if translated_terms.present?

    keys.each do |key|
      translation = self.translate_with_auto_scope "terms#{sep}#{key}", :raise => true rescue nil
      next unless translation.is_a? String

      translated_terms["terms#{sep}#{key}"] = translation
{'fraction_non_alphanumeric': 0.11319092710856639, 'fraction_numerical': 0.011451222197753798, 'mean_word_length': 3.5602409638554215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4649952', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1495, 'n_words': 355}