class PhraseApp::InContextEditor::Delegate::I18nDelegate

Attributes

api_client[RW]
display_key[RW]
fallback_keys[RW]
key[RW]
options[RW]
original_args[RW]

Public Class Methods

new(key, options={}, original_args=nil) click to toggle source
Calls superclass method
# File lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb, line 16
def initialize(key, options={}, original_args=nil)
  @key = key
  @options = options
  @original_args = original_args
  @display_key = DisplayableKeyIdentifier.new(api_wrapper).identify(@key, @options)
  super(decorated_key_name)
end

Public Instance Methods

method_missing(*args, &block) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb, line 24
def method_missing(*args, &block)
  self.class.log "Trying to execute missing method ##{args.first} on key #{@key}"
  if @key.respond_to?(args.first)
    to_s.send(*args)
  else
    data = translation_or_subkeys
    if data.respond_to?(args.first)
      data.send(*args, &block)
    else
      self.class.log "You tried to execute the missing method ##{args.first} on key #{@key} which is not supported. Please make sure you treat your translations as strings only."
      original_translation = ::I18n.translate_without_phraseapp(*@original_args)
      original_translation.send(*args, &block)
    end
  end
end

Private Instance Methods

api_wrapper() click to toggle source
# File lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb, line 74
def api_wrapper
  @api_wrapper ||= InContextEditor::ApiWrapper.new
end
translation_content_for_key(key) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb, line 60
def translation_content_for_key(key)
  default_translations = api_wrapper.default_translation(key)
  return unless default_translations.present?

  if key.plural
    default_translations.inject({}) do |hash, translation|
      hash[translation.plural_suffix.to_sym] = translation.content
      hash
    end
  else
    default_translations.first.content
  end
end
translation_or_subkeys() click to toggle source
# File lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb, line 42
def translation_or_subkeys
  return nil if @key.nil? || @key.to_s == ""

  keys = api_wrapper.keys_with_prefix(@key)
  return nil unless keys.present?

  if keys.size == 1
    translation_content_for_key(keys.first)
  else
    translation_hash = keys.inject({}) do |hash, key|
      hash[key.name] = translation_content_for_key(key)
      hash
    end

    PhraseApp::InContextEditor::HashFlattener.expand_flat_hash(translation_hash, @key)
  end
end