class I18n::Hygiene::VariableChecker

Checks for mismatching interpolation variables. For example, if the value for an i18n key as defined in :en contains an interpolation variable, the value for that key as defined in any other locale must have a matching variable name.

Public Class Methods

new(key, i18n_wrapper, primary_locale, locales = []) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 7
def initialize(key, i18n_wrapper, primary_locale, locales = [])
  @key = key
  @i18n_wrapper = i18n_wrapper
  @primary_locale = primary_locale
  @locales = locales
end

Public Instance Methods

mismatched_variables() { |locale, key, missing_variables(locale)| ... } click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 14
def mismatched_variables
  @locales.each { |locale| yield locale, @key, missing_variables(locale) }
end

Private Instance Methods

collect_variables(string) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 34
def collect_variables(string)
  return [] unless string.is_a?(String)
  (rails_variables(string) + js_variables(string)).uniq.sort
end
js_variables(string) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 43
def js_variables(string)
  without_markdown_italics(string.scan(/__\S+__/).map { |var_string| var_string.gsub("__", "").to_sym })
end
key_defined?(locale) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 26
def key_defined?(locale)
  @i18n_wrapper.key_found?(locale, @key)
end
missing_variables(locale) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 20
def missing_variables(locale)
  return [] unless key_defined?(locale)

  variables(@primary_locale).reject { |v| variables(locale).include?(v) }
end
rails_variables(string) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 39
def rails_variables(string)
  string.scan(/%{\S+?}/).map { |var_string| var_string.gsub(/[%{}]/, '').to_sym }.uniq
end
variables(locale) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 30
def variables(locale)
  collect_variables(@i18n_wrapper.value(locale, @key))
end
without_markdown_italics(array) click to toggle source
# File lib/i18n/hygiene/variable_checker.rb, line 47
def without_markdown_italics(array)
  @key.end_with?("_markdown") ? [] : array
end