Skip to content

Commit cbf827b

Browse files
committed
Merge pull request rails#22462 from lxsameer/i18n_html_wrap
wrapping i18n missing keys made optional
2 parents 7eae0bb + c1dbb13 commit cbf827b

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

actionview/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* `I18n.translate` helper will wrap the missing translation keys
2+
in a <span> tag only if `debug_missing_translation` configuration
3+
be true. Default value is `true`. For example in `application.rb`:
4+
5+
# in order to turn off missing key wrapping
6+
config.action_view.debug_missing_tranlation = false
7+
8+
*Sameer Rahmani*
9+
110
* Respect value of `:object` if `:object` is false when rendering.
211

312
Fixes #22260.

actionview/lib/action_view/helpers/translation_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ module ActionView
66
# = Action View Translation Helpers
77
module Helpers
88
module TranslationHelper
9+
extend ActiveSupport::Concern
10+
911
include TagHelper
12+
13+
included do
14+
mattr_accessor :debug_missing_translation
15+
self.debug_missing_translation = true
16+
end
17+
1018
# Delegates to <tt>I18n#translate</tt> but also performs three additional
1119
# functions.
1220
#
@@ -95,6 +103,8 @@ def translate(key, options = {})
95103
title << ", " << interpolations.map { |k, v| "#{k}: #{ERB::Util.html_escape(v)}" }.join(', ')
96104
end
97105

106+
return title unless ActionView::Base.debug_missing_translation
107+
98108
content_tag('span', keys.last.to_s.titleize, class: 'translation_missing', title: title)
99109
end
100110
end

actionview/lib/action_view/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module ActionView
66
class Railtie < Rails::Railtie # :nodoc:
77
config.action_view = ActiveSupport::OrderedOptions.new
88
config.action_view.embed_authenticity_token_in_remote_forms = false
9+
config.action_view.debug_missing_translation = true
910

1011
config.eager_load_namespaces << ActionView
1112

actionview/test/template/translation_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def test_delegates_localize_to_i18n
5454
end
5555
end
5656

57+
def test_returns_missing_tranlation_message_without_span_wrap
58+
old_value = ActionView::Base.debug_missing_translation
59+
ActionView::Base.debug_missing_translation = false
60+
61+
expected = 'translation missing: en.translations.missing'
62+
assert_equal expected, translate(:"translations.missing")
63+
ensure
64+
ActionView::Base.debug_missing_translation = old_value
65+
end
66+
5767
def test_returns_missing_translation_message_wrapped_into_span
5868
expected = '<span class="translation_missing" title="translation missing: en.translations.missing">Missing</span>'
5969
assert_equal expected, translate(:"translations.missing")

guides/source/configuring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ encrypted cookies salt value. Defaults to `'signed encrypted cookie'`.
461461
* `config.action_view.automatically_disable_submit_tag` determines whether
462462
submit_tag should automatically disable on click, this defaults to true.
463463

464+
* `config.action_view.debug_missing_translation` determins whether to wrap the missing translations key in a `<span>` tag or not. This defaults to true.
465+
464466
### Configuring Action Mailer
465467

466468
There are a number of settings available on `config.action_mailer`:

0 commit comments

Comments
 (0)