Skip to content
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
6 changes: 6 additions & 0 deletions app/models/concerns/discourse_translator/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ def set_detected_locale(locale)
(content_locale || build_content_locale).update!(detected_locale: locale)
end

# This method is used to create a translation for a translatable (Post or Topic) and a specific locale.
# If a translation already exists for the locale, it will be updated.
# Texts are put through a Sanitizer to clean them up before saving.
# @param locale [String] the locale of the translation
# @param text [String] the translated text
def set_translation(locale, text)
locale = locale.to_s.gsub("_", "-")
text = DiscourseTranslator::TranslatedContentSanitizer.sanitize(text)
translations.find_or_initialize_by(locale: locale).update!(translation: text)
end

Expand Down
9 changes: 9 additions & 0 deletions lib/discourse_translator/translated_content_sanitizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module DiscourseTranslator
class TranslatedContentSanitizer
def self.sanitize(content)
PrettyText.cleanup(content, {})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sanitizer only calls prettytext for now.

In general I would be moving towards creating specific classes as we may see this function expand.

end
end
end
12 changes: 12 additions & 0 deletions spec/lib/translated_content_sanitizer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

describe DiscourseTranslator::TranslatedContentSanitizer do
it "sanitizes the content" do
sanitized =
DiscourseTranslator::TranslatedContentSanitizer.sanitize(
"<script>alert('test')</script><p> <h1>Testing</h1> This is a test post</p>",
)

expect(sanitized).to eq("<p> </p><h1>Testing</h1> This is a test post<p></p>")
end
end
Loading