Sha256: 6a62d0f46e11e05ce040d8a5af5e101799eabe2c62db65642c57b86e78fdcb5b

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

class TranslationEngine::Translation

  def self.catched
    @catched ||= []
  end

  def self.clear_catched
    @catched = []
  end

  def self.catch(text, keys)
    if text.is_a?(Hash)
      text.each do |key, value|
        TranslationEngine::Translation.catch(value, keys + [key])
      end
    else
      catch_basic_value(text, keys)
    end
  end

  def self.catch_basic_value(text, keys)
    catched << {
      data_type: data_type(text),
      key:       keys.join('.'),
      text:      normalize_for_translation_server(text)
    }
  end

  def self.normalize_for_translation_server(value)
    if value.is_a?(Array)
      YAML.dump(value).gsub("---\n", '')
    else
      value
    end
  end

  def self.data_type(text)
    case text
    when Array      then 'array'
    when String     then 'string'
    when Float      then 'float'
    when Integer    then 'integer'
    when TrueClass  then 'boolean'
    when FalseClass then 'boolean'
    when Symbol     then 'symbol'
    else nil
    end
  end

  def initialize(text, keys)
    @text = text
    @keys = keys
    self.class.catch(@text, @keys)
  end

  def full_key
    @keys.join('.') rescue ''
  end

  def to_s
    if TranslationEngine.use_screenshots && @text.is_a?(String)
      "--TRANSLATION--#{full_key}--#{@text}"
    else
      @text
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
translation_engine-0.0.5 app/models/translation_engine/translation.rb
translation_engine-0.0.4 app/models/translation_engine/translation.rb
translation_engine-0.0.3 app/models/translation_engine/translation.rb
translation_engine-0.0.2 app/models/translation_engine/translation.rb
translation_engine-0.0.1 app/models/translation_engine/translation.rb