Sha256: e773c73fd93db54f9836096608dc88089f12ef5f69870e8e75be4687fa70b39e

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require 'fast_gettext'

module ForemanThemeSatellite
  # This repository is a wrapper above other repository,
  # it replaces branded words with their downstream counterparts.
  class ReplacerRepository < ::FastGettext::TranslationRepository::Base
    def initialize(old_repo)
      @repo = old_repo
    end

    def pluralisation_rule
      @repo.pluralisation_rule
    end

    def available_locales
      @repo.available_locales
    end

    def [](key)
      original = @repo[key]
      val = original || key
      return original unless val.is_a? String

      val = val.dup if val

      replaced = replace_string(val)

      replaced == val ? original : replaced
    end

    def plural(*keys)
      @repo.plural(*keys)
    end

    def reload
      @repo.reload
    end

    private

    # 'babcb'.split(/b/, -1) => ['', 'a', 'c', ''].join('z') => 'zazcz'
    def replace_string(val)
      return '' if val.empty?

      ForemanThemeSatellite::FOREMAN_BRAND.each do |foreman_word, value|
        parts = val.split(foreman_word, -1)
        next if parts.size == 1 # no match, try the next word

        parts = parts.map { |part| replace_string(part) }
        return parts.join(value)
      end

      val # no match found at all
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman_theme_satellite-14.2.0 lib/foreman_theme_satellite/replacer_repository.rb
foreman_theme_satellite-14.1.0 lib/foreman_theme_satellite/replacer_repository.rb
foreman_theme_satellite-14.0.1 lib/foreman_theme_satellite/replacer_repository.rb
foreman_theme_satellite-14.0.0 lib/foreman_theme_satellite/replacer_repository.rb