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
2 changes: 2 additions & 0 deletions app/helpers/topics_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def categories_breadcrumb(topic)
end

def localize_topic_view_content(topic_view)
return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)

crawl_locale = params[Discourse::LOCALE_PARAM].presence || SiteSetting.default_locale

LocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, crawl_locale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ export default class TopicLocalizedContentToggle extends Component {
});
}

this.router.refresh();
const postStream = this.args.topic?.postStream;
if (postStream) {
const currentURL = this.router.currentURL;
// this is required to clear the post stream cache
// otherwise the old posts before the toggle will be shown
postStream.removeAllPosts();
await this.router.refresh();
// refreshing clears the post number,
// and postStream.refresh nearPost does not load to the correct post
if (this.router.currentURL !== currentURL) {
this.router.replaceWith(currentURL);
}
}
}

get title() {
Expand Down
5 changes: 5 additions & 0 deletions frontend/discourse/app/models/post-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,11 @@ export default class PostStream extends RestModel {
postIds.forEach((id) => delete identityMap[id]);
}

removeAllPosts() {
const posts = Object.values(this._identityMap).filter((p) => p);
this.removePosts(posts);
}

// Returns a post from the identity map if it's been inserted.
findLoadedPost(id) {
return this._identityMap[id];
Expand Down
78 changes: 78 additions & 0 deletions spec/requests/topics_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3637,6 +3637,84 @@ def extract_post_stream
end
end
end

describe "#posts with content localization" do
fab!(:localized_post) do
post = Fabricate(:post, user:, locale: "en", cooked: "<p>Original EN</p>")
Fabricate(:post_localization, post:, locale: "ja", cooked: "<p>Translated JA</p>")
post
end
fab!(:localized_topic) { localized_post.topic }
fab!(:localized_post2) do
post =
Fabricate(
:post,
user:,
topic: localized_topic,
locale: "ja",
cooked: "<p>Original 2 JA</p>",
)
Fabricate(:post_localization, post:, locale: "en", cooked: "<p>Translated 2 EN</p>")
post
end

before do
SiteSetting.content_localization_enabled = true
I18n.locale = "en"
end

context "when show_original cookie is not set" do
it "returns translated posts" do
get "/t/#{localized_topic.id}/posts.json"

expect(response.status).to eq(200)

body = response.parsed_body
posts = body["post_stream"]["posts"]

expect(posts.first["cooked"]).to eq("<p>Original EN</p>")
expect(posts.second["cooked"]).to eq("<p>Translated 2 EN</p>")
end

it "returns translated posts when loading specific post_ids" do
get "/t/#{localized_topic.id}/posts.json", params: { post_ids: [localized_post2.id] }

expect(response.status).to eq(200)

body = response.parsed_body
posts = body["post_stream"]["posts"]

expect(posts.first["cooked"]).to eq("<p>Translated 2 EN</p>")
end
end

context "when show_original cookie is set" do
before { cookies[ContentLocalization::SHOW_ORIGINAL_COOKIE] = "true" }

it "returns original posts" do
get "/t/#{localized_topic.id}/posts.json"

expect(response.status).to eq(200)

body = response.parsed_body
posts = body["post_stream"]["posts"]

expect(posts.first["cooked"]).to eq("<p>Original EN</p>")
expect(posts.second["cooked"]).to eq("<p>Original 2 JA</p>")
end

it "returns original posts when loading specific post_ids" do
get "/t/#{localized_topic.id}/posts.json", params: { post_ids: [localized_post2.id] }

expect(response.status).to eq(200)

body = response.parsed_body
posts = body["post_stream"]["posts"]

expect(posts.first["cooked"]).to eq("<p>Original 2 JA</p>")
end
end
end
end

describe "#feed" do
Expand Down
57 changes: 57 additions & 0 deletions spec/system/content_localization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,63 @@
expect(post_history_modal.previous_locale).to have_content("English (US)")
end
end

context "when loading 20+ posts in stream", trace: true do
before do
highest = topic.highest_post_number
22.times do |i|
post_number = i + highest + 1
post =
Fabricate(
:post,
topic: topic,
locale: "ja",
raw: "Japanese content for post #{post_number}",
cooked: "<p>日本語コンテンツ #{post_number}</p>",
)

Fabricate(
:post_localization,
post:,
locale: "en",
cooked: "<p>English translation #{post_number}</p>",
)
end
end

let(:post_21_obj) { PageObjects::Components::Post.new(21) }

it "respects the show_original toggle for posts loaded dynamically when scrolling (20+ posts)" do
sign_in(site_local_user)
visit("/")

topic_page.visit_topic(topic)

expect(post_3_obj.post).to have_content("A general is one who ..")
expect(topic_page).to have_post_content(post_number: 3, content: "A general is one who ..")

5.times do
break if page.has_css?("#post_21 .cooked", visible: :all, wait: 0)
page.execute_script("window.scrollTo(0, document.body.scrollHeight)")
end

expect(page).to have_css("#post_21")
expect(topic_page).to have_post_content(post_number: 21, content: "English translation 21")

page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
expect(post_3_obj.post).to have_content("将とは、智・信・仁・勇・厳なり。")

5.times do
break if page.has_css?("#post_21 .cooked", visible: :all, wait: 0)
page.execute_script("window.scrollTo(0, document.body.scrollHeight)")
end

expect(post_21_obj.post).to have_content("日本語コンテンツ 21")

page.refresh
expect(post_3_obj.post).to have_content("将とは、智・信・仁・勇・厳なり。")
end
end
end

context "for site settings" do
Expand Down