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
10 changes: 5 additions & 5 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,32 +203,32 @@

add_to_serializer(:basic_category, :name) do
object.uncategorized? ? I18n.t('uncategorized_category_name', locale: SiteSetting.default_locale) :
((scope && scope.current_user ? Multilingual::Translation.get("category_name", object.slug_path)[scope.current_user.locale.to_sym] :
((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_name", object.slug_path)[scope.current_user.locale.to_sym] :
object.name) || object.name)
end

add_to_serializer(:basic_category, :description_text) do
object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) :
((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
object.description_text) || object.description_text)
end

add_to_serializer(:basic_category, :description) do
object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) :
((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
object.description) || object.description)
end

add_to_serializer(:basic_category, :description_excerpt) do
object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) :
((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] :
object.description_excerpt) || object.description_excerpt)
end

add_to_serializer(:site, :categories) do
object.categories.map do |c|
c[:name] = c[:slug] == "uncategorized" ? I18n.t('uncategorized_category_name', locale: SiteSetting.default_locale) :
((scope && scope.current_user && c[:slug_path] ? Multilingual::Translation.get("category_name", c[:slug_path])[scope.current_user.locale.to_sym] :
((scope && scope.current_user && scope.current_user.locale && c[:slug_path] ? Multilingual::Translation.get("category_name", c[:slug_path])[scope.current_user.locale.to_sym] :
c[:name]) || c[:name])
c.to_h
end
Expand Down
41 changes: 41 additions & 0 deletions spec/serializers/site_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

describe SiteSerializer do
fab!(:user) { Fabricate(:user, admin: false) }
let(:guardian) { Guardian.new(user) }
let(:category) { Fabricate(:category) }

after do
Site.clear_cache
end

it "serializes names as translations" do
Multilingual::CustomTranslation.create(
file_name: "category_name.wbp.yml",
file_type: "category_name",
locale: "wbp",
file_ext: "yml",
translation_data: { category.slug => "pardu-pardu-mani" }
)
user.locale = "wbp"
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
c1 = serialized[:categories].find { |c| c[:id] == category.id }

expect(c1[:name]).to eq("pardu-pardu-mani")
end

it "doesn't explode when user locale is nil" do
Multilingual::CustomTranslation.create(
file_name: "category_name.wbp.yml",
file_type: "category_name",
locale: "wbp",
file_ext: "yml",
translation_data: { category.slug => "pardu-pardu-mani" }
)
user.locale = nil
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
c1 = serialized[:categories].find { |c| c[:id] == category.id }

expect(c1[:name]).to eq(category.name)
end
end