Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Ensure old feature works with new and show translate button in correct scenarios #215

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 8 additions & 10 deletions lib/discourse_translator/guardian_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ def can_detect_language?(post)
end

def can_translate?(post)
return false if !user_group_allow_translate?
return false unless user_group_allow_translate?

locale = post.detected_locale
return false if locale.nil?

# I18n.locale is a symbol e.g. :en_GB
detected_lang = locale.to_s.sub("-", "_")
detected_lang != I18n.locale.to_s &&
"DiscourseTranslator::#{SiteSetting.translator}".constantize.language_supported?(
detected_lang,
)
if SiteSetting.experimental_topic_translation
return false if post.translation_for(I18n.locale).present?
return false if post.locale_matches?(I18n.locale)
true
else
poster_group_allow_translate?(post)
end
end
end
96 changes: 73 additions & 23 deletions spec/lib/guardian_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,42 +130,92 @@
describe "when translator enabled" do
before { SiteSetting.translator_enabled = true }

describe "anon user" do
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
describe "when experimental_topic_translation enabled" do
before { SiteSetting.experimental_topic_translation = true }

it "cannot translate" do
expect(Guardian.new.can_translate?(post)).to eq(false)
describe "anon user" do
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }

it "cannot translate" do
expect(Guardian.new.can_translate?(post)).to eq(false)
end
end

describe "logged in user" do
it "cannot translate when user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"

expect(guardian.can_translate?(post)).to eq(false)
end

describe "user is in restrict_translation_by_group" do
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }

describe "locale is :pt" do
before { I18n.stubs(:locale).returns(:pt) }

it "cannot translate when post detected locale matches i18n locale" do
post.set_detected_locale("pt")

expect(guardian.can_translate?(post)).to eq(false)
end

it "can translate when post's detected locale does not match i18n locale" do
post.set_detected_locale("jp")

expect(guardian.can_translate?(post)).to eq(true)
end

it "can translate when post does not have translation" do
Copy link
Contributor

Choose a reason for hiding this comment

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

The setup for this test is exactly the same as the test before it. I think the set up for this test should be clearing out the post's translation?

Copy link
Contributor Author

@nattsw nattsw Feb 18, 2025

Choose a reason for hiding this comment

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

The post will not have translation if I don't explicitly add post.set_translation(locale, text)

It's exactly the same setup, but my intent is that the test description is the documentation of the behaviour not the implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for clarifying 👍 Perhaps we can just combine the two test cases into one? Technically we can only translate when post's detected locale does not match i18n locale and also when the post does not have translation.

Might also be useful to assert that the post cannot be translated when the post's translation is present for the given locale.

Copy link
Contributor Author

@nattsw nattsw Feb 18, 2025

Choose a reason for hiding this comment

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

the post cannot be translated when the post's translation is present for the given locale

Actually, "can_translate" is the bool that determines if the 🌐 appears on the post.

(Old feature) If the translation exists for the locale, the value should still be true, since the user will click on 🌐 and we will return the existing translation to the user. This is the old feature that we need to maintain, where viewing translations are manual, not the new feature where it is automatically rendered by default.

old feature

Screenshot 2025-02-18 at 2 44 32 PM

new experimental feature

Screenshot 2025-02-18 at 2 47 33 PM

post.set_detected_locale("jp")

expect(guardian.can_translate?(post)).to eq(true)
end
end
end
end
end

describe "logged in user" do
it "cannot translate when user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
describe "when experimental topic translation disabled" do
before { SiteSetting.experimental_topic_translation = false }

describe "anon user" do
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }

expect(guardian.can_translate?(post)).to eq(false)
it "cannot translate" do
expect(Guardian.new.can_translate?(post)).to eq(false)
end
end

describe "user is in restrict_translation_by_group" do
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
describe "logged in user" do
it "cannot translate when user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"

describe "locale is :xx" do
before { I18n.stubs(:locale).returns(:pt) }
expect(guardian.can_translate?(post)).to eq(false)
end

it "cannot translate when post does not have detected locale" do
expect(post.detected_locale).to eq(nil)
expect(guardian.can_translate?(post)).to eq(false)
end
describe "user is in restrict_translation_by_group" do
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }

it "cannot translate when post detected locale matches i18n locale" do
post.set_detected_locale("pt")
describe "locale is :pt" do
before { I18n.stubs(:locale).returns(:pt) }

expect(guardian.can_translate?(post)).to eq(false)
end
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"

expect(guardian.can_translate?(post)).to eq(false)
end

it "can translate if poster is in restrict_translation_by_poster_group" do
poster = post.user
poster_group = Fabricate(:group, users: [poster])

it "can translate when post detected locale does not match i18n locale" do
post.set_detected_locale("jp")
SiteSetting.restrict_translation_by_poster_group = "#{poster_group.id}"
expect(guardian.can_translate?(post)).to eq(true)

expect(guardian.can_translate?(post)).to eq(true)
SiteSetting.restrict_translation_by_poster_group = ""
expect(guardian.can_translate?(post)).to eq(true)
end
end
end
end
Expand Down
Loading