<commit_before>module Rafinesque
  module I18nBackend

    def self.included(base)
      base.class_eval do
        attr_accessor :default_semantics
        alias :store_translations_without_rafinesque :store_translations
        alias :store_translations :store_translations_with_rafinesque
      end
    end

    def store_translations_with_rafinesque(locale, data, options = {})
      data = deep_map(data) { |string| fill_placeholders(string, locale) }
      store_translations_without_rafinesque(locale, data, options)
    end

    private

    def deep_map node, &block
      case node
      when String then yield(node)
      when Array
        node.each_with_index do |obj, i|
          node[i]= deep_map(obj, &block)
        end
      when Hash
        node.each_pair do |key, value|
          node[key]= deep_map(value, &block)
        end
      end
      node
    end

    def fill_placeholders(string, locale)
      semantics[locale.to_s].each_pair do |search,replace|
        string.sub!("$#{search}$", replace)
      end
      string
    end

    def semantics
      @semantics ||= Hash.new({}).merge! default_semantics
    end
  end
end

I18n.backend.extend Rafinesque::I18nBackend
<commit_msg>use gsub instead of sub<commit_after>module Rafinesque
  module I18nBackend

    def self.included(base)
      base.class_eval do
        attr_accessor :default_semantics
        alias :store_translations_without_rafinesque :store_translations
        alias :store_translations :store_translations_with_rafinesque
      end
    end

    def store_translations_with_rafinesque(locale, data, options = {})
      data = deep_map(data) { |string| fill_placeholders(string, locale) }
      store_translations_without_rafinesque(locale, data, options)
    end

    private

    def deep_map node, &block
      case node
      when String then yield(node)
      when Array
        node.each_with_index do |obj, i|
          node[i]= deep_map(obj, &block)
        end
      when Hash
        node.each_pair do |key, value|
          node[key]= deep_map(value, &block)
        end
      end
      node
    end

    def fill_placeholders(string, locale)
      semantics[locale.to_s].each_pair do |search,replace|
        string.gsub!("$#{search}$", replace)
      end
      string
    end

    def semantics
      @semantics ||= Hash.new({}).merge! default_semantics
    end
  end
end

I18n.backend.extend Rafinesque::I18nBackend

{'fraction_non_alphanumeric': 0.07904487443392343, 'fraction_numerical': 0.004940304652120214, 'mean_word_length': 2.6986301369863015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7366526', 'n_tokens_mistral': 773, 'n_tokens_neox': 750, 'n_words': 176}