class TranslationBuffer

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 7
def initialize(*)
  @translate_flag = true
  @options = {}
  super
end

Public Instance Methods

i18n(virtual_path = nil)
internationalization(virtual_path = nil) click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 28
def internationalization virtual_path = nil
  @virtual_path = virtual_path
  translate_flag? ? translate(self, @options).translate_false : self
end
Also aliased as: i18n
internationalization_options(options = {}) click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 16
def internationalization_options options = {}
  @options = options
  self
end
Also aliased as: io
io(options = {})
to_s() click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 35
def to_s
  # これをやらないと to_s した時に String に戻る
  self
end
translate_false() click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 23
def translate_false
  @translate_flag = false
  self
end
translate_flag?() click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 12
def translate_flag?
  defined?(@translate_flag) && @translate_flag
end

Private Instance Methods

scope_key_by_partial(key) click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 89
def scope_key_by_partial(key)
  if key.to_s.first == "."
    if @virtual_path
      @virtual_path.gsub(%r{/_?}, ".") + key.to_s
    else
      raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
    end
  else
    key
  end
end
t(key, options = {})
Alias for: translate
translate(key, options = {}) click to toggle source
# File lib/amelia/active_support/core_ext/translation_buffer.rb, line 41
def translate(key, options = {})
  options = options.dup
  has_default = options.has_key?(:default)
  remaining_defaults = Array(options.delete(:default)).compact

  if has_default && !remaining_defaults.first.kind_of?(Symbol)
    options[:default] = remaining_defaults
  end

  # If the user has explicitly decided to NOT raise errors, pass that option to I18n.
  # Otherwise, tell I18n to raise an exception, which we rescue further in this method.
  # Note: `raise_error` refers to us re-raising the error in this method. I18n is forced to raise by default.
  if options[:raise] == false || (options.key?(:rescue_format) && options[:rescue_format].nil?)
    raise_error = false
    i18n_raise = false
  else
    raise_error = options[:raise] || options[:rescue_format] || ActionView::Base.raise_on_missing_translations
    i18n_raise = true
  end

  if key.to_s =~ /(\b|_|\.)html$/
    html_safe_options = options.dup
    options.except(*I18n::RESERVED_KEYS).each do |name, value|
      unless name == :count && value.is_a?(Numeric)
        html_safe_options[name] = ERB::Util.html_escape(value.to_s)
      end
    end
    translation = I18n.translate(scope_key_by_partial(key), html_safe_options.merge(raise: i18n_raise))

    translation.respond_to?(:html_safe) ? translation.html_safe : translation
  else
    I18n.translate(scope_key_by_partial(key), options.merge(raise: i18n_raise))
  end
rescue I18n::MissingTranslationData => e
  if remaining_defaults.present?
    translate remaining_defaults.shift, options.merge(default: remaining_defaults)
  else
    raise e if raise_error

    keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
    keys.last.to_s
  end
rescue I18n::ArgumentError => e
  key
end
Also aliased as: t