Sha256: 8f0f1772246627a9b9918b5009c5e2c3a040ce5100ab345775b763d608780792

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Babelyoda
  class LengthChecker
    def initialize(dev_language, params)
      @dev_language = dev_language
      @params = params
    end

    def long_translations(keyset)
      long_translations = {}

      keyset.keys.each_value do |key|
        dev_value = key.values[@dev_language]
        dev_text = longest_translation(dev_value)
        dev_len = dev_text.length.to_f

        key.values.each_value do |value|
          text = longest_translation(value)
          len = text.nil? ? 0 : text.length
          ratio = len/dev_len
          big_ratio = ratio >= @params.ratio
          big_delta = (len - dev_len) >= @params.delta

          if big_ratio && big_delta
            lang = value.language.to_sym
            translation = Babelyoda::LongTranslation.new(dev_text, text, key.context)
            if long_translations.has_key?(lang)
              long_translations[lang].push(translation)
            else
              long_translations[lang] = [translation]
            end
          end
        end
      end
      long_translations
    end

  private

    def longest_translation(localizaition_value)
      if localizaition_value.plural?
        translation = localizaition_value.text.values.max_by {|str| str.nil? ? 0 : str.length}
      else
        translation = localizaition_value.text
      end
      translation
    end
  end

  class LongTranslation
    attr_accessor :dev_text
    attr_accessor :text
    attr_accessor :context
    
    def initialize(dev_text, text, context)
      @dev_text = dev_text
      @text = text
      @context = context
    end
    
    def ==(other_obj)
      dev_texts_equal = @dev_text == other_obj.dev_text
      texts_equal = @text == other_obj.text
      contexts_equal = @context == other_obj.context
      dev_texts_equal && texts_equal && contexts_equal
   end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
babelyoda-2.2.0 lib/babelyoda/length_checker.rb