Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
< 3.5.0.beta2-dev: 5f24835801fdc7cb98e1bcf42d2ab2e49e609921
Copy link
Contributor Author

@nattsw nattsw Feb 25, 2025

Choose a reason for hiding this comment

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

Made a mistake in my previous compat file update...

We are on 3.5.0.beta2-dev now, and the commit before this one is 5f24835801fdc7cb98e1bcf42d2ab2e49e609921.

This PR uses PrettyText.cleanup(content, {}) which is new in core.

< 3.5.0.beta1-dev: 7d411e458bdd449f8aead2bc07cedeb00b856798
< 3.4.0.beta3-dev: b4cf3a065884816fa3f770248c2bf908ba65d8ac
< 3.4.0.beta1-dev: 5346b4bafba2c2fb817f030a473b7bbca97b909c
Expand Down
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