class Ndd::RSpec::Rails::Matchers::TranslationMatcher
Base class for matchers dealing with translation.
Public Class Methods
Set the locales to test to I18n.available_locales
.
# File lib/ndd/rspec/rails/matchers/translation_matcher.rb, line 13 def initialize @tested_locales = I18n.available_locales end
Public Instance Methods
Set the locales to test to all the available locales (i.e. I18n.available_locales
). @return self
# File lib/ndd/rspec/rails/matchers/translation_matcher.rb, line 19 def in_available_locales @tested_locales = I18n.available_locales self end
Set the locales to test to the default locale (i.e. I18n.default_locale
) only. @return self
# File lib/ndd/rspec/rails/matchers/translation_matcher.rb, line 26 def in_default_locale @tested_locales = [I18n.default_locale] self end
Private Instance Methods
Convert an array of locales to a human readable list, i.e. ':en, :fr, :jp'. @param locales [Array<String|Symbol>] the locales to convert. @return [String] the converted locales.
# File lib/ndd/rspec/rails/matchers/translation_matcher.rb, line 37 def locales_as_string(locales) locales.map { |locale| ":#{locale}" }.join(', ') end
Check that a translation exists for the given key in the given locale. @param locale [Symbol] the locale of the translation to lookup. @param key [String] the key of the translation to lookup. @return [Boolean] true if a translation exists for the given key in the given locale, false otherwise.
# File lib/ndd/rspec/rails/matchers/translation_matcher.rb, line 45 def translated?(locale, key) I18n.with_locale(locale) { I18n.translate!(key, fallback: false) } return true rescue I18n::MissingTranslationData return false end