Skip to content

Commit

Permalink
Merge pull request #654 from Nerian/add-options-to-missing-trabslatio…
Browse files Browse the repository at this point in the history
…n-message

Add more helpful translation error when :default option is provided.
  • Loading branch information
radar committed Apr 26, 2023
2 parents 4ced6ee + c5c6e75 commit 8dfcb53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 8dfcb53

Please sign in to comment.