From bc13bb747ab231a5fd50d8b74f58cf6f03b24d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Saraiva?= Date: Tue, 20 Mar 2018 12:32:05 +0000 Subject: [PATCH 1/3] Support for i18n :html subkeys in help text --- lib/bootstrap_form/form_builder.rb | 8 +++++++- test/bootstrap_form_group_test.rb | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 8347596ff..ceca432a1 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -539,7 +539,13 @@ def get_help_text_by_i18n_key(name) underscored_scope = "activerecord.help.#{partial_scope.underscore}" downcased_scope = "activerecord.help.#{partial_scope.downcase}" - help_text = I18n.t(name, scope: underscored_scope, default: '').presence + # First check for a subkey :html, as it is also accepted by i18n, and the simple check for name would return an hash instead of a string (both with .presence returning true!) + help_text = I18n.t("#{name}.html", scope: underscored_scope, default: '').html_safe.presence + help_text ||= if text = I18n.t("#{name}.html", scope: downcased_scope, default: '').html_safe.presence + warn "I18n key '#{downcased_scope}.#{name}' is deprecated, use '#{underscored_scope}.#{name}' instead" + text + end + help_text ||= I18n.t(name, scope: underscored_scope, default: '').presence help_text ||= if text = I18n.t(name, scope: downcased_scope, default: '').presence warn "I18n key '#{downcased_scope}.#{name}' is deprecated, use '#{underscored_scope}.#{name}' instead" text diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index 1dafd3033..44dfbb5c8 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -207,6 +207,27 @@ class BootstrapFormGroupTest < ActionView::TestCase assert_equivalent_xml expected, @builder.text_field(:password) end + test "help messages to look up I18n automatically using HTML key" do + I18n.backend.store_translations(:en, activerecord: { + help: { + user: { + password: { + html: 'A good password should be at least six characters long' + } + } + } + }) + + expected = <<-HTML.strip_heredoc +
+ + + A good password should be at least six characters long +
+ HTML + assert_equivalent_xml expected, @builder.text_field(:password) + end + test "help messages to warn about deprecated I18n key" do super_user = SuperUser.new(@user.attributes) builder = BootstrapForm::FormBuilder.new(:super_user, super_user, self, {}) From 21d9b93359a0109158665461928d2f8d34d293fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Saraiva?= Date: Tue, 20 Mar 2018 12:49:00 +0000 Subject: [PATCH 2/3] added info to changelog, as per bot instructions --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e4e0453b..41c9cbcb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ In addition to these necessary markup changes, the bootstrap_form API itself has [@duleorlovic](https://github.com/duleorlovic) * Your contribution here! +* [#455](https://github.com/bootstrap-ruby/bootstrap_form/pull/455): Support for i18n :html subkeys in help text - [@jsaraiva](https://github.com/jsaraiva). + ## [2.7.0][] (2017-04-21) Features: From 734550d39c4e603388507e92446915d4fb690562 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Wed, 21 Mar 2018 08:49:17 -0700 Subject: [PATCH 3/3] Tweak changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c9cbcb0..0b2cb7caf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ In addition to these necessary markup changes, the bootstrap_form API itself has * Support Bootstrap v4's [Custom Checkboxes and Radios](https://getbootstrap.com/docs/4.0/components/forms/#checkboxes-and-radios-1) with a new `custom: true` option * Allow HTML in help translations by using the `_html` suffix on the key - [@unikitty37](https://github.com/unikitty37) * [#408](https://github.com/bootstrap-ruby/bootstrap_form/pull/408): Add option[:id] on static control #245 - [@duleorlovic](https://github.com/duleorlovic). +* [#455](https://github.com/bootstrap-ruby/bootstrap_form/pull/455): Support for i18n `:html` subkeys in help text - [@jsaraiva](https://github.com/jsaraiva). * Your contribution here! ### Bugfixes @@ -35,7 +36,6 @@ In addition to these necessary markup changes, the bootstrap_form API itself has [@duleorlovic](https://github.com/duleorlovic) * Your contribution here! -* [#455](https://github.com/bootstrap-ruby/bootstrap_form/pull/455): Support for i18n :html subkeys in help text - [@jsaraiva](https://github.com/jsaraiva). ## [2.7.0][] (2017-04-21)