Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Set category localizations in category settings (#32412)

Keegan George10c817fab5a2
diff --git a/app/assets/javascripts/discourse/app/components/edit-category-localizations.gjs b/app/assets/javascripts/discourse/app/components/edit-category-localizations.gjs
new file mode 100644
index 00000000000..1ca42a54a09
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/edit-category-localizations.gjs
@@ -0,0 +1,99 @@
+import { fn, hash } from "@ember/helper";
+import { service } from "@ember/service";
+import { eq } from "truth-helpers";
+import { buildCategoryPanel } from "discourse/components/edit-category-panel";
+import { i18n } from "discourse-i18n";
+
+export default class EditCategoryLocalizations extends buildCategoryPanel(
+ "localizations"
+) {
+ @service siteSettings;
+
+ get availableLocales() {
+ return JSON.parse(this.siteSettings.available_locales);
+ }
+
+ <template>
+ {{#if (eq @transientData.localizations.length 0)}}
+ <@form.Alert @icon="circle-info">
+ {{i18n "category.localization.hint"}}
+ </@form.Alert>
+ {{/if}}
+
+ <@form.Collection @name="localizations" as |collection index|>
+ <collection.Field
+ @name="category_id"
+ @title="category_id"
+ @showTitle={{false}}
+ @disabled={{true}}
+ as |field|
+ >
+ <field.Input @value={{this.category.id}} @type="hidden" />
+ </collection.Field>
+
+ <@form.Row as |row|>
+ <row.Col @size={{2}}>
+ <collection.Field
+ @name="locale"
+ @title={{i18n "category.localization.locale"}}
+ @format="full"
+ as |field|
+ >
+ <field.Select as |select|>
+ {{#each this.availableLocales as |locale|}}
+ <select.Option
+ @value={{locale.value}}
+ >{{locale.name}}</select.Option>
+ {{/each}}
+ </field.Select>
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{4}}>
+ <collection.Field
+ @name="name"
+ @title={{i18n "category.localization.name"}}
+ @validation="length:1,50"
+ as |field|
+ >
+ <field.Input
+ placeholder={{i18n "category.name_placeholder"}}
+ @maxlength="50"
+ class="category-name"
+ />
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{5}}>
+ <collection.Field
+ @name="description"
+ @title={{i18n "category.localization.description"}}
+ as |field|
+ >
+ <field.Textarea @height={{60}} />
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{1}}>
+ <@form.Button
+ class="btn-danger remove-localization"
+ @icon="trash-can"
+ @title="category.localization.remove"
+ @action={{fn collection.remove index}}
+ />
+ </row.Col>
+ </@form.Row>
+ </@form.Collection>
+
+ <@form.Button
+ class="add-localization"
+ @icon="plus"
+ @label="category.localization.add"
+ @action={{fn
+ @form.addItemToCollection
+ "localizations"
+ (hash category_id=this.category.id locale="" name="" description="")
+ }}
+ />
+ </template>
+}
diff --git a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
index 3e6e7e40f0f..f271231efb9 100644
--- a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
+++ b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
@@ -22,6 +22,7 @@ const FIELD_LIST = [
"style_type",
"emoji",
"icon",
+ "localizations",
];
export default class EditCategoryTabsController extends Controller {
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index 38c63ec4d12..25793e099f6 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -456,6 +456,7 @@ export default class Category extends RestModel {
@tracked styleType = this.style_type;
@tracked emoji;
@tracked icon;
+ @tracked localizations = this.category_localizations;
permissions = null;
init() {
@@ -781,11 +782,28 @@ export default class Category extends RestModel {
style_type: this.style_type,
emoji: this.emoji,
icon: this.icon,
+ ...(this.siteSettings.experimental_content_localization && {
+ category_localizations_attributes: this._buildUpdatedLocalizations(),
+ }),
}),
type: id ? "PUT" : "POST",
});
}
+ _buildUpdatedLocalizations() {
+ const localizationsToDestroy = this.category_localizations
+ .filter(
+ (original) =>
+ !this.localizations.some((newLoc) => newLoc.id === original.id)
+ )
+ .map((loc) => ({
+ id: loc.id,
+ _destroy: true,
+ }));
+
+ return [...this.localizations, ...localizationsToDestroy];
+ }
+
_permissionsForUpdate() {
const permissions = this.permissions;
let rval = {};
diff --git a/app/assets/javascripts/discourse/app/routes/new-category.js b/app/assets/javascripts/discourse/app/routes/new-category.js
index 18c95d9c420..e50e92af8e4 100644
--- a/app/assets/javascripts/discourse/app/routes/new-category.js
+++ b/app/assets/javascripts/discourse/app/routes/new-category.js
@@ -58,6 +58,7 @@ export default class NewCategory extends DiscourseRoute {
required_tag_groups: [],
form_template_ids: [],
minimum_required_tags: 0,
+ category_localizations: [],
});
}
diff --git a/app/assets/javascripts/discourse/app/templates/edit-category-tabs.hbs b/app/assets/javascripts/discourse/app/templates/edit-category-tabs.hbs
index 5814a4ae433..7dd3f710372 100644
--- a/app/assets/javascripts/discourse/app/templates/edit-category-tabs.hbs
+++ b/app/assets/javascripts/discourse/app/templates/edit-category-tabs.hbs
@@ -62,6 +62,15 @@
@tab="tags"
/>
{{/if}}
+
+ {{#if this.siteSettings.experimental_content_localization}}
+ <EditCategoryTab
+ @panels={{this.panels}}
+ @selectedTab={{this.selectedTab}}
+ @params={{this.parentParams}}
+ @tab="localizations"
+ />
+ {{/if}}
</ul>
</div>
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index fadec681f48..7402253db60 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -539,6 +539,12 @@ class CategoriesController < ApplicationController
conditional_param_keys << { moderating_group_ids: [] }
end
+ if SiteSetting.experimental_content_localization?
+ conditional_param_keys << {
+ category_localizations_attributes: %i[id category_id locale name description _destroy],
+ }
+ end
+
result =
params.permit(
*required_param_keys,
diff --git a/app/models/category.rb b/app/models/category.rb
index 537312dd6da..171bc27c984 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -62,6 +62,7 @@ class Category < ActiveRecord::Base
has_and_belongs_to_many :web_hooks
accepts_nested_attributes_for :category_setting, update_only: true
+ accepts_nested_attributes_for :category_localizations, allow_destroy: true
validates :user_id, presence: true
diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb
index 27d5df64fa6..875c8070fe0 100644
--- a/app/serializers/category_serializer.rb
+++ b/app/serializers/category_serializer.rb
@@ -8,6 +8,10 @@ class CategorySerializer < SiteCategorySerializer
:require_topic_approval
end
+ class CategoryLocalizationSerializer < ApplicationSerializer
+ attributes :id, :category_id, :locale, :name, :description
+ end
+
attributes :read_restricted,
:available_groups,
:auto_close_hours,
@@ -33,6 +37,7 @@ class CategorySerializer < SiteCategorySerializer
:icon
has_one :category_setting, serializer: CategorySettingSerializer, embed: :objects
+ has_many :category_localizations, serializer: CategoryLocalizationSerializer, embed: :objects
def include_moderating_group_ids?
SiteSetting.enable_category_group_moderation?
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 195af0f12e6..3eef4e48d8f 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4125,6 +4125,14 @@ en:
settings: "Settings"
topic_template: "Template"
tags: "Tags"
+ localizations: "Localizations"
+ localization:
+ locale: "Locale"
+ name: "Name"
+ description: "Description"
+ add: "Add new locale"
+ remove: "Remove"
+ hint: "There are currently no localizations for this category. Click 'Add new locale' below to begin adding localizations."
tags_allowed_tags: "Restrict these tags to this category:"
tags_allowed_tag_groups: "Restrict these tag groups to this category:"
tags_placeholder: "(Optional) list of allowed tags"
diff --git a/spec/requests/api/schemas/json/category_create_response.json b/spec/requests/api/schemas/json/category_create_response.json
index 049959e6a7f..d7e0e6c0316 100644
--- a/spec/requests/api/schemas/json/category_create_response.json
+++ b/spec/requests/api/schemas/json/category_create_response.json
@@ -187,6 +187,7 @@
"require_reply_approval": null,
"require_topic_approval": null
},
+ "category_localizations": [],
"read_only_banner": {
"type": [
"string",
diff --git a/spec/requests/api/schemas/json/category_update_response.json b/spec/requests/api/schemas/json/category_update_response.json
index 936e909f60e..e555e41ba0a 100644
--- a/spec/requests/api/schemas/json/category_update_response.json
+++ b/spec/requests/api/schemas/json/category_update_response.json
@@ -190,6 +190,7 @@
"require_reply_approval": null,
"require_topic_approval": null
},
+ "category_localizations": [],
"read_only_banner": {
"type": [
"string",
diff --git a/spec/system/edit_category_localizations_spec.rb b/spec/system/edit_category_localizations_spec.rb
new file mode 100644
index 00000000000..2ee998df0b6
--- /dev/null
+++ b/spec/system/edit_category_localizations_spec.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+describe "Edit Category Localizations", type: :system do
+ fab!(:admin)
+ fab!(:category) do
+ Fabricate(:category, name: "Feature Requests", slug: "feature-requests", topic_count: 1234)
+ end
+ let(:category_page) { PageObjects::Pages::Category.new }
+ let(:form) { PageObjects::Components::FormKit.new("form") }
+
+ before { sign_in(admin) }
+
+ context "when content localization setting is disabled" do
+ before { SiteSetting.experimental_content_localization = false }
+
+ it "should not show the localization tab" do
+ category_page.visit_settings(category)
+ expect(category_page).to have_no_setting_tab("localizations")
+ end
+ end
+
+ context "when content localization setting is enabled" do
+ before { SiteSetting.experimental_content_localization = true }
+
+ it "should show the localization tab" do
+ category_page.visit_settings(category)
+ expect(category_page).to have_setting_tab("localizations")
+ end
+
+ describe "when editing a category with no category localizations" do
+ before { category.update(category_localizations: []) }
+
+ it "should show info hint to add new localizations" do
+ category_page.visit_edit_localizations(category)
+ expect(form).to have_an_alert(I18n.t("js.category.localization.hint"))
+ end
+
+ it "should allow you to add new localizations" do
+ category_page.visit_edit_localizations(category)
+ category_page.find(".edit-category-tab-localizations .add-localization").click
+ form.field("localizations.0.locale").select("es")
+ form.field("localizations.0.name").fill_in("Solicitud de función")
+ form.field("localizations.0.description").fill_in(
+ "Solicitar nuevas funcionalidades en esta categoría",
+ )
+ category_page.find(".edit-category-tab-localizations .add-localization").click
+ form.field("localizations.1.locale").select("fr")
+ form.field("localizations.1.name").fill_in("Demande de fonctionnalité")
+ form.field("localizations.1.description").fill_in(
+ "Demander de nouvelles fonctionnalités dans cette catégorie",
+ )
+ category_page.save_settings
+ page.refresh
+
+ expect(CategoryLocalization.where(category_id: category.id).count).to eq(2)
+ expect(CategoryLocalization.where(category_id: category.id, locale: "es").count).to eq(1)
+ expect(CategoryLocalization.where(category_id: category.id, locale: "fr").count).to eq(1)
+ expect(CategoryLocalization.where(category_id: category.id, locale: "es").first.name).to eq(
+ "Solicitud de función",
+ )
+ expect(CategoryLocalization.where(category_id: category.id, locale: "fr").first.name).to eq(
+ "Demande de fonctionnalité",
+ )
+ expect(
+ CategoryLocalization.where(category_id: category.id, locale: "es").first.description,
+ ).to eq("Solicitar nuevas funcionalidades en esta categoría")
+ expect(
+ CategoryLocalization.where(category_id: category.id, locale: "fr").first.description,
+ ).to eq("Demander de nouvelles fonctionnalités dans cette catégorie")
+ end
+ end
+
+ describe "when editing a category with localizations" do
+ fab!(:category_localization) { Fabricate(:category_localization, category: category) }
+
+ it "should allow you to delete localizations" do
+ expect(CategoryLocalization.where(category_id: category.id).count).to eq(1)
+ category_page.visit_edit_localizations(category)
+ page.find(".edit-category-tab-localizations .remove-localization").click
+ category_page.save_settings
+ page.refresh
+ expect(CategoryLocalization.where(category_id: category.id).count).to eq(0)
+ end
+ end
+ end
+end
diff --git a/spec/system/page_objects/pages/category.rb b/spec/system/page_objects/pages/category.rb
index ec528559596..0b79e8d996a 100644
--- a/spec/system/page_objects/pages/category.rb
+++ b/spec/system/page_objects/pages/category.rb
@@ -20,6 +20,10 @@ module PageObjects
self
end
+ def visit_edit_localizations(category)
+ page.visit("/c/#{category.slug}/edit/localizations")
+ end
+
def visit_categories
page.visit("/categories")
self
@@ -105,6 +109,16 @@ module PageObjects
def has_no_public_access_message?
page.has_no_content?(I18n.t("js.category.permissions.everyone_has_access"))
end
+
+ def has_setting_tab?(tab_name)
+ tab_css = ".edit-category-#{tab_name}"
+ page.has_css?(tab_css)
+ end
+
+ def has_no_setting_tab?(tab_name)
+ tab_css = ".edit-category-#{tab_name}"
+ page.has_no_css?(tab_css)
+ end
end
end
end