Skip to content

Commit 9d241e6

Browse files
authored
Merge pull request rails#52487 from shouichi/raise-on-missing-translations-strict
Change human_attribute_name to raise an error iff in strict mode
2 parents b408864 + 8b511de commit 9d241e6

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

activesupport/lib/active_support/i18n_railtie.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def self.initialize_i18n(app)
4949
when :load_path
5050
I18n.load_path += value
5151
when :raise_on_missing_translations
52-
setup_raise_on_missing_translations_config(app)
52+
strict = value == :strict
53+
setup_raise_on_missing_translations_config(app, strict)
5354
else
5455
I18n.public_send("#{setting}=", value)
5556
end
@@ -78,13 +79,13 @@ def self.initialize_i18n(app)
7879
@i18n_inited = true
7980
end
8081

81-
def self.setup_raise_on_missing_translations_config(app)
82+
def self.setup_raise_on_missing_translations_config(app, strict)
8283
ActiveSupport.on_load(:action_view) do
8384
ActionView::Helpers::TranslationHelper.raise_on_missing_translations = app.config.i18n.raise_on_missing_translations
8485
end
8586

8687
ActiveSupport.on_load(:active_model_translation) do
87-
ActiveModel::Translation.raise_on_missing_translations = app.config.i18n.raise_on_missing_translations
88+
ActiveModel::Translation.raise_on_missing_translations = app.config.i18n.raise_on_missing_translations if strict
8889
end
8990

9091
if app.config.i18n.raise_on_missing_translations &&

guides/source/configuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ Sets the path Rails uses to look for locale files. Defaults to `config/locales/*
937937

938938
#### `config.i18n.raise_on_missing_translations`
939939

940-
Determines whether an error should be raised for missing translations. This defaults to `false`.
940+
Determines whether an error should be raised for missing translations. If `true`, views and controllers raise `I18n::MissingTranslationData`. If `:strict`, models also raise the error. This defaults to `false`.
941941

942942
#### `config.i18n.fallbacks`
943943

guides/source/i18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ The I18n API defines the following exceptions that will be raised by backends wh
11501150

11511151
#### Customizing how `I18n::MissingTranslationData` is handled
11521152

1153-
If `config.i18n.raise_on_missing_translations` is `true`, `I18n::MissingTranslationData` errors will be raised. It's a good idea to turn this on in your test environment, so you can catch places where missing translations are requested.
1153+
If `config.i18n.raise_on_missing_translations` is `true`, `I18n::MissingTranslationData` errors will be raised from views and controllers. If the value is `:strict`, models also raise the error. It's a good idea to turn this on in your test environment, so you can catch places where missing translations are requested.
11541154

11551155
If `config.i18n.raise_on_missing_translations` is `false` (the default in all environments), the exception's error message will be printed. This contains the missing key/scope so you can fix your code.
11561156

railties/test/application/configuration_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,8 +4633,8 @@ def view_test
46334633
assert_includes last_response.body, "rescued missing translation error from view"
46344634
end
46354635

4636-
test "raise_on_missing_translations affects human_attribute_name in model" do
4637-
add_to_config "config.i18n.raise_on_missing_translations = true"
4636+
test "raise_on_missing_translations = :strict affects human_attribute_name in model" do
4637+
add_to_config "config.i18n.raise_on_missing_translations = :strict"
46384638

46394639
app_file "app/models/post.rb", <<-RUBY
46404640
class Post < ActiveRecord::Base
@@ -4648,6 +4648,21 @@ class Post < ActiveRecord::Base
46484648
end
46494649
end
46504650

4651+
test "raise_on_missing_translations = true does not affect human_attribute_name in model" do
4652+
add_to_config "config.i18n.raise_on_missing_translations = true"
4653+
4654+
app_file "app/models/post.rb", <<-RUBY
4655+
class Post < ActiveRecord::Base
4656+
end
4657+
RUBY
4658+
4659+
app "development"
4660+
4661+
assert_nothing_raised do
4662+
Post.human_attribute_name("title")
4663+
end
4664+
end
4665+
46514666
test "dom testing uses the HTML5 parser in new apps if it is supported" do
46524667
app "development"
46534668
expected = defined?(Nokogiri::HTML5) ? :html5 : :html4

0 commit comments

Comments
 (0)