Skip to content

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

FEATURE: Add category locale field to localizations tab (#38852)

Natalie Tay4b8ee9599453
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index 66a5702dc0b..3f0cd507cee 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -597,6 +597,7 @@ class CategoriesController < ApplicationController
end
if SiteSetting.content_localization_enabled?
+ conditional_param_keys << :locale
conditional_param_keys << { category_localizations: %i[id locale name description] }
end
diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb
index 285723b7aef..1dad9018998 100644
--- a/app/serializers/category_serializer.rb
+++ b/app/serializers/category_serializer.rb
@@ -14,7 +14,8 @@ class CategorySerializer < SiteCategorySerializer
attributes :id, :locale, :name, :description
end
- attributes :read_restricted,
+ attributes :locale,
+ :read_restricted,
:available_groups,
:auto_close_hours,
:auto_close_based_on_last_post,
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 2ee12e26e24..189b085dd03 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4699,6 +4699,8 @@ en:
tags: "Tags"
localizations: "Localizations"
localization:
+ language: "Category language"
+ language_description: "The primary language of this category's name and description."
locale: "Locale"
name: "Name"
description: "Description"
diff --git a/frontend/discourse/admin/components/edit-category-localizations.gjs b/frontend/discourse/admin/components/edit-category-localizations.gjs
index 1b51af8761b..47dd6573a9a 100644
--- a/frontend/discourse/admin/components/edit-category-localizations.gjs
+++ b/frontend/discourse/admin/components/edit-category-localizations.gjs
@@ -26,80 +26,101 @@ export default class EditCategoryLocalizations extends buildCategoryPanel(
}
<template>
- {{#if (eq @transientData.localizations.length 0)}}
- <@form.Alert @icon="circle-info">
- {{i18n "category.localization.hint"}}
- </@form.Alert>
- {{/if}}
+ <@form.Section>
+ <@form.Field
+ @name="locale"
+ @title={{i18n "category.localization.language"}}
+ @description={{i18n "category.localization.language_description"}}
+ @format="large"
+ @type="select"
+ as |field|
+ >
+ <field.Control as |select|>
+ {{#each this.selectableLocales as |locale|}}
+ <select.Option
+ @value={{locale.value}}
+ >{{locale.name}}</select.Option>
+ {{/each}}
+ </field.Control>
+ </@form.Field>
+ </@form.Section>
- <@form.Collection @name="localizations" as |collection index|>
- <@form.Row as |row|>
- <row.Col @size={{2}}>
- <collection.Field
- @name="locale"
- @title={{i18n "category.localization.locale"}}
- @format="full"
- @validation="required"
- @type="select"
- as |field|
- >
- <field.Control as |select|>
- {{#each this.selectableLocales as |locale|}}
- <select.Option
- @value={{locale.value}}
- >{{locale.name}}</select.Option>
- {{/each}}
- </field.Control>
- </collection.Field>
- </row.Col>
+ <@form.Section @title={{i18n "category.localizations"}}>
+ {{#if (eq @transientData.localizations.length 0)}}
+ <@form.Alert @icon="circle-info">
+ {{i18n "category.localization.hint"}}
+ </@form.Alert>
+ {{/if}}
- <row.Col @size={{4}}>
- <collection.Field
- @name="name"
- @title={{i18n "category.localization.name"}}
- @validation="required|length:1,50"
- @type="input"
- as |field|
- >
- <field.Control
- placeholder={{i18n "category.name_placeholder"}}
- @maxlength="50"
- class="category-name"
- />
- </collection.Field>
- </row.Col>
+ <@form.Collection @name="localizations" as |collection index|>
+ <@form.Row as |row|>
+ <row.Col @size={{2}}>
+ <collection.Field
+ @name="locale"
+ @title={{i18n "category.localization.locale"}}
+ @format="full"
+ @validation="required"
+ @type="select"
+ as |field|
+ >
+ <field.Control as |select|>
+ {{#each this.selectableLocales as |locale|}}
+ <select.Option
+ @value={{locale.value}}
+ >{{locale.name}}</select.Option>
+ {{/each}}
+ </field.Control>
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{4}}>
+ <collection.Field
+ @name="name"
+ @title={{i18n "category.localization.name"}}
+ @validation="required|length:1,50"
+ @type="input"
+ as |field|
+ >
+ <field.Control
+ 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"}}
- @type="textarea"
- as |field|
- >
- <field.Control @height={{60}} />
- </collection.Field>
- </row.Col>
+ <row.Col @size={{5}}>
+ <collection.Field
+ @name="description"
+ @title={{i18n "category.localization.description"}}
+ @type="textarea"
+ as |field|
+ >
+ <field.Control @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>
+ <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="btn-default add-localization"
- @icon="plus"
- @label="category.localization.add"
- @action={{fn
- @form.addItemToCollection
- "localizations"
- (hash category_id=this.category.id locale="" name="" description="")
- }}
- />
+ <@form.Button
+ class="btn-default add-localization"
+ @icon="plus"
+ @label="category.localization.add"
+ @action={{fn
+ @form.addItemToCollection
+ "localizations"
+ (hash category_id=this.category.id locale="" name="" description="")
+ }}
+ />
+ </@form.Section>
</template>
}
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index 8e0240dfd2e..a0bae9c0eae 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -25,6 +25,7 @@ const LEGACY_FORMKIT_FIELDS = [
"style_type",
"emoji",
"icon",
+ "locale",
"localizations",
"email_in",
"email_in_enabled",
@@ -46,6 +47,7 @@ const SIMPLIFIED_FIELD_LIST = [
"style_type",
"emoji",
"icon",
+ "locale",
"localizations",
"position",
"num_featured_topics",
@@ -126,6 +128,10 @@ export default class EditCategoryTabsController extends Controller {
: LEGACY_FORMKIT_FIELDS)
);
+ if (this.siteSettings.content_localization_enabled && !data.locale) {
+ data.locale = this.siteSettings.default_locale;
+ }
+
if (enableSimplifiedCategoryCreation) {
if (!this.model.styleType) {
data.style_type = "icon";
diff --git a/frontend/discourse/app/models/category.js b/frontend/discourse/app/models/category.js
index 962eab9b078..b4886f17216 100644
--- a/frontend/discourse/app/models/category.js
+++ b/frontend/discourse/app/models/category.js
@@ -841,6 +841,7 @@ export default class Category extends RestModel {
emoji: this.emoji,
icon: this.icon,
...(this.siteSettings.content_localization_enabled && {
+ locale: this.locale,
category_localizations: this.localizations,
}),
...this._categoryTypeSaveProperties(id),
diff --git a/spec/requests/api/schemas/json/category_create_response.json b/spec/requests/api/schemas/json/category_create_response.json
index ae840596e2a..7367f567e47 100644
--- a/spec/requests/api/schemas/json/category_create_response.json
+++ b/spec/requests/api/schemas/json/category_create_response.json
@@ -35,6 +35,12 @@
"slug": {
"type": "string"
},
+ "locale": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
"topic_count": {
"type": "integer"
},
diff --git a/spec/requests/api/schemas/json/category_update_response.json b/spec/requests/api/schemas/json/category_update_response.json
index 610f67225cb..e34769a07e5 100644
--- a/spec/requests/api/schemas/json/category_update_response.json
+++ b/spec/requests/api/schemas/json/category_update_response.json
@@ -38,6 +38,12 @@
"slug": {
"type": "string"
},
+ "locale": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
"topic_count": {
"type": "integer"
},
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index 6c4efe4b80e..6843e7bdb0b 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -1235,6 +1235,22 @@ RSpec.describe CategoriesController do
expect(SiteSetting.max_category_nesting).to eq(3)
end
end
+
+ it "updates locale when content_localization_enabled" do
+ SiteSetting.content_localization_enabled = true
+
+ put "/categories/#{category.id}.json", params: { locale: "ja" }
+ expect(response.status).to eq(200)
+ expect(category.reload.locale).to eq("ja")
+ end
+
+ it "does not update locale when content_localization_enabled is false" do
+ SiteSetting.content_localization_enabled = false
+
+ put "/categories/#{category.id}.json", params: { locale: "ja" }
+ expect(response.status).to eq(200)
+ expect(category.reload.locale).to be_nil
+ end
end
end
diff --git a/spec/system/category_localizations_spec.rb b/spec/system/category_localizations_spec.rb
index 053d0ba1908..b3965dd7b8c 100644
--- a/spec/system/category_localizations_spec.rb
+++ b/spec/system/category_localizations_spec.rb
@@ -73,6 +73,29 @@ describe "Category Localizations" do
expect(category_page).to have_setting_tab("localizations")
end
+ it "defaults to site locale when category has no locale" do
+ category_without_locale = Fabricate(:category, locale: nil)
+ category_page.visit_edit_localizations(category_without_locale)
+
+ expect(form.field("locale")).to have_value(SiteSetting.default_locale)
+ end
+
+ it "loads the saved locale correctly" do
+ category_page.visit_edit_localizations(category)
+
+ expect(form.field("locale")).to have_value("en")
+ end
+
+ it "allows setting and persisting the category locale" do
+ category_without_locale = Fabricate(:category, locale: nil)
+ category_page.visit_edit_localizations(category_without_locale)
+
+ form.field("locale").select("ja")
+ category_page.save_settings
+
+ expect(category_without_locale.reload.locale).to eq("ja")
+ end
+
describe "when editing a category with no category localizations" do
fab!(:mono_category, :category)
@@ -136,7 +159,9 @@ describe "Category Localizations" do
count: 2,
)
expect(
- page.all(".form-kit__control-select option.--selected").map(&:text),
+ page.all(".form-kit__collection .form-kit__control-select option.--selected").map(
+ &:text
+ ),
).to contain_exactly("Spanish (Español)", "Japanese (日本語)")
page.find(".edit-category-tab-localizations .remove-localization", match: :first).click