Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more helpful translation error when :default option is provided. #654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/i18n/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize(filename, exception_message)

class MissingTranslation < ArgumentError
module Base
PERMITTED_KEYS = [:scope].freeze
PERMITTED_KEYS = [:scope, :default].freeze

attr_reader :locale, :key, :options

Expand All @@ -63,8 +63,18 @@ def keys
end

def message
"translation missing: #{keys.join('.')}"
if options[:default].is_a?(Array)
other_options = ([key, *options[:default]]).map { |k| normalized_option(k).prepend('- ') }.join("\n")
"Translation missing. Options considered were:\n#{other_options}"
else
"Translation missing: #{keys.join('.')}"
end
end

def normalized_option(key)
I18n.normalize_keys(locale, key, options[:scope]).join('.')
end

alias :to_s :message

def to_exception
Expand Down
6 changes: 6 additions & 0 deletions test/i18n/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_invalid_locale_stores_locale
end
end

test "MissingTranslationData message contains all potential options" do
force_missing_translation_data(default: [:option_a, :option_b]) do |exception|
assert_equal "translation missing. Options considered were:\n- de.bar.option_a, \n- de.bar.option_a", exception.message
end
end

test "InvalidPluralizationData stores entry, count and key" do
force_invalid_pluralization_data do |exception|
assert_equal({:other => "bar"}, exception.entry)
Expand Down