diff --git a/CHANGELOG.md b/CHANGELOG.md index d407ccdb..4d04e8bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ### Unreleased +* Support html safe translations for Rails 7+ + ### 1.6.3 * Fix `debug` and `ignore_failure` options in `post` requests. #284 - @mike-burns diff --git a/lib/noticed/translation.rb b/lib/noticed/translation.rb index ff8bd8f0..44571def 100644 --- a/lib/noticed/translation.rb +++ b/lib/noticed/translation.rb @@ -2,6 +2,8 @@ module Noticed module Translation extend ActiveSupport::Concern + include ActiveSupport::HtmlSafeTranslation if defined?(ActiveSupport::HtmlSafeTranslation) + # Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup. def i18n_scope :notifications @@ -12,7 +14,11 @@ def class_scope end def translate(key, **options) - I18n.translate(scope_translation_key(key), **options) + if defined?(super) + super scope_translation_key(key), **options + else + I18n.translate scope_translation_key(key), **options + end end alias_method :t, :translate diff --git a/test/dummy/config/environments/development.rb b/test/dummy/config/environments/development.rb index 09692df0..150fcfe2 100644 --- a/test/dummy/config/environments/development.rb +++ b/test/dummy/config/environments/development.rb @@ -45,14 +45,6 @@ # Highlight code that triggered database queries in logs. config.active_record.verbose_query_logs = true - # Debug mode disables concatenation and preprocessing of assets. - # This option may cause significant delays in view rendering with a large - # number of complex assets. - config.assets.debug = true - - # Suppress logger output for asset requests. - config.assets.quiet = true - # Raises error for missing translations. # config.action_view.raise_on_missing_translations = true diff --git a/test/dummy/config/locales/en.yml b/test/dummy/config/locales/en.yml index aed50484..676340a8 100644 --- a/test/dummy/config/locales/en.yml +++ b/test/dummy/config/locales/en.yml @@ -31,6 +31,7 @@ en: hello: "Hello world" + message_html: "

Hello world

" notifications: noticed: i18n_example: diff --git a/test/translation_test.rb b/test/translation_test.rb index 250d1f43..8be0dfdb 100644 --- a/test/translation_test.rb +++ b/test/translation_test.rb @@ -5,6 +5,10 @@ class I18nExample < Noticed::Base def message t("hello") end + + def html_message + t("message_html") + end end class Noticed::I18nExample < Noticed::Base @@ -37,4 +41,12 @@ def message assert_equal "noticed.scoped_i18n_example.message", ScopedI18nExample.new.send(:scope_translation_key, ".message") assert_equal "This is a custom scoped translation", ScopedI18nExample.new.message end + + if defined?(ActiveSupport::HtmlSafeTranslation) + test "I18n supports html safe translations" do + message = I18nExample.new.html_message + assert_equal "

Hello world

", message + assert message.html_safe? + end + end end