Skip to content

Commit

Permalink
Support for i18n :html subkeys in help text (bootstrap-ruby#455)
Browse files Browse the repository at this point in the history
* Support for i18n :html subkeys in help text
* added info to changelog, as per bot instructions
* Tweak changelog
  • Loading branch information
jsaraiva authored and xire28 committed Mar 30, 2018
1 parent a0c1eba commit 881cb2d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>good</strong> password should be at least six characters long'
}
}
}
})

expected = <<-HTML.strip_heredoc
<div class="form-group">
<label for="user_password">Password</label>
<input class="form-control" id="user_password" name="user[password]" type="text" value="secret" />
<small class="form-text text-muted">A <strong>good</strong> password should be at least six characters long</small>
</div>
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, {})
Expand Down

0 comments on commit 881cb2d

Please sign in to comment.