Skip to content

Commit e635b47

Browse files
authored
FIX: Set a stripped post's locale to site default locale (#212)
In posts where there is a just an image, the stripped text that is sent for locale detection can be ... ``` <p></p> ``` ... as we do not send HTML for the image for locale detection. The stripped text would result in a `""` returned for detected locale, which would result in ``` Detected locale can't be blank ``` This commit defaults the post to the site's locale. This is needed so that we do not keep sending the post to check the locale.
1 parent ed168d2 commit e635b47

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/services/discourse_translator/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def self.get_detected_locale(translatable)
8888
end
8989

9090
def self.save_detected_locale(translatable)
91-
detected_locale = yield
91+
# sometimes we may have a user post that is just an emoji
92+
# in that case, we will just indicate the post is in the default locale
93+
detected_locale = yield.presence || SiteSetting.default_locale
9294
translatable.set_detected_locale(detected_locale)
9395

9496
detected_locale

spec/services/base_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ class EmptyTranslator < DiscourseTranslator::Base
115115
expect(TestTranslator.detect(post)).to eq("en")
116116
end
117117

118+
it "saves the site default locale when detection is empty" do
119+
SiteSetting.default_locale = "ja"
120+
121+
TestTranslator.save_detected_locale(post) { "" }
122+
123+
expect(post.detected_locale).to eq("ja")
124+
end
125+
118126
it "performs detection if no cached result" do
119127
TestTranslator.expects(:detect!).with(post).returns("es")
120128

0 commit comments

Comments
 (0)