Skip to content

Commit

Permalink
Add support for HTML translations. Fixes #310 (#311)
Browse files Browse the repository at this point in the history
Allows users to use _html translations that will be automatically marked as html safe.
  • Loading branch information
excid3 authored Oct 12, 2023
1 parent fdc3293 commit 8864bbc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/noticed/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
8 changes: 0 additions & 8 deletions test/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

en:
hello: "Hello world"
message_html: "<p>Hello world</p>"
notifications:
noticed:
i18n_example:
Expand Down
12 changes: 12 additions & 0 deletions test/translation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "<p>Hello world</p>", message
assert message.html_safe?
end
end
end

0 comments on commit 8864bbc

Please sign in to comment.