FIX: Return properly interpolated translations for flag types

Currently, descriptions for flag types aren’t interpolated, returning
`%{base_path}` in their string, for example. This breaks the navigation
on the sites.

The behavior changed probably because of an upgrade of Ruby, as two
hashes were passed to `I18n.t` (`vars` and `default`) without using the
splat operator.
This commit is contained in:
Loïc Guitaut 2024-07-30 17:22:43 +02:00 committed by Loïc Guitaut
parent a3d61ba1c4
commit 335ab115b3
3 changed files with 29 additions and 13 deletions

View File

@ -31,16 +31,14 @@ class PostActionTypeSerializer < ApplicationSerializer
def description def description
i18n( i18n(
"description", "description",
vars: { tos_url:,
tos_url:, base_path: Discourse.base_path,
base_path: Discourse.base_path,
},
default: object.class.descriptions[object.id], default: object.class.descriptions[object.id],
) )
end end
def short_description def short_description
i18n("short_description", vars: { tos_url: tos_url, base_path: Discourse.base_path }) i18n("short_description", tos_url:, base_path: Discourse.base_path, default: "")
end end
def name_key def name_key
@ -60,10 +58,14 @@ class PostActionTypeSerializer < ApplicationSerializer
ReviewableScore.exists?(reviewable_score_type: object.id) ReviewableScore.exists?(reviewable_score_type: object.id)
end end
protected private
def i18n(field, default: nil, vars: nil) def i18n(field, **args)
key = "post_action_types.#{name_key}.#{field}" key = "#{i18n_prefix}.#{name_key}.#{field}"
vars ? I18n.t(key, vars, default: default) : I18n.t(key, default: default) I18n.t(key, **args)
end
def i18n_prefix
"post_action_types"
end end
end end

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class TopicFlagTypeSerializer < PostActionTypeSerializer class TopicFlagTypeSerializer < PostActionTypeSerializer
protected private
def i18n(field, default: nil, vars: nil) def i18n_prefix
key = "topic_flag_types.#{name_key}.#{field}" "topic_flag_types"
vars ? I18n.t(key, vars, default: default) : I18n.t(key, default: default)
end end
end end

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
RSpec.describe PostActionTypeSerializer do
subject(:serializer) { described_class.new(post_action_type, scope: Guardian.new, root: false) }
let(:post_action_type) { PostActionType.find_by(name_key: :inappropriate) }
describe "#description" do
before { Discourse.stubs(:base_path).returns("discourse.org") }
it "returns properly interpolated translation" do
expect(serializer.description).to match(%r{discourse\.org/guidelines})
end
end
end