From 881cb2d0532668ebeccd7818319ca4dd6775d79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Saraiva?= Date: Wed, 21 Mar 2018 15:55:00 +0000 Subject: [PATCH] Support for i18n :html subkeys in help text (#455) * Support for i18n :html subkeys in help text * added info to changelog, as per bot instructions * Tweak changelog --- CHANGELOG.md | 2 ++ lib/bootstrap_form/form_builder.rb | 8 +++++++- test/bootstrap_form_group_test.rb | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e4e0453b..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,6 +36,7 @@ In addition to these necessary markup changes, the bootstrap_form API itself has [@duleorlovic](https://github.com/duleorlovic) * Your contribution here! + ## [2.7.0][] (2017-04-21) Features: diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 725491eb0..e29fdbdd3 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -541,7 +541,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, {})