module DryI18n

Constants

VERSION

Public Instance Methods

replace_variable_in_translation(translation) click to toggle source
# File lib/dry_i18n.rb, line 22
def replace_variable_in_translation(translation)
  variables = variables?(translation)

  if variables
    variables.each do |variable|
      key = variable[/@\[(.*?)\]/m, 1]
      options = key.scan(/\{.*?\}/)

      if options.any?
        options = eval(options.first)
        key = key.split(',').first
      else
        options = {}
      end

      translation = translation.sub(variable, I18n.translate(key, options))
    end
  end

  translation
end
translate(locale, key, options = {}) click to toggle source
# File lib/dry_i18n.rb, line 8
def translate(locale, key, options = {})
  translation = original_translate(locale, key, options)

  if translation.is_a? Array
    translation.each_with_index do |translation_subpart, i|
      translation[i] = replace_variable_in_translation(translation_subpart) if translation_subpart.is_a? String
    end
  elsif translation.is_a? String
    translation = replace_variable_in_translation(translation)
  end

  translation
end
variables?(translation) click to toggle source
# File lib/dry_i18n.rb, line 46
def variables?(translation)
  nested_words = translation.scan(/\@\[.*?\]/)
  if !nested_words.any?
    false
  else
    nested_words
  end
end