Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for i18n :html subkeys in help text #455

Merged
merged 3 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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 @@ -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
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