1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix generator suggestion raise error when I18n.available_locales don’t include :en

This commit is contained in:
willnet 2017-07-22 00:01:40 +09:00
parent 4d5f0bb30b
commit 424debb096
2 changed files with 15 additions and 1 deletions

View file

@ -274,8 +274,9 @@ module Rails
else
options = sorted_groups.flat_map(&:last)
suggestions = options.sort_by { |suggested| levenshtein_distance(namespace.to_s, suggested) }.first(3)
suggestions.map! { |s| "'#{s}'" }
msg = "Could not find generator '#{namespace}'. ".dup
msg << "Maybe you meant #{ suggestions.map { |s| "'#{s}'" }.to_sentence(last_word_connector: " or ", locale: :en) }\n"
msg << "Maybe you meant #{ suggestions[0...-1].join(', ')} or #{suggestions[-1]}\n"
msg << "Run `rails generate --help` for more options."
puts msg
end

View file

@ -36,6 +36,19 @@ class GeneratorsTest < Rails::Generators::TestCase
assert_match "Maybe you meant 'migration'", output
end
def test_generator_suggestions_except_en_locale
orig_available_locales = I18n.available_locales
orig_default_locale = I18n.default_locale
I18n.available_locales = :ja
I18n.default_locale = :ja
name = :tas
output = capture(:stdout) { Rails::Generators.invoke name }
assert_match "Maybe you meant 'task', 'job' or", output
ensure
I18n.available_locales = orig_available_locales
I18n.default_locale = orig_default_locale
end
def test_generator_multiple_suggestions
name = :tas
output = capture(:stdout) { Rails::Generators.invoke name }