From dc996a1e5c3af3b78f16a97cb912ae6941011f26 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com> Date: Fri, 19 Apr 2024 19:23:27 +0200 Subject: [PATCH] FIX: Locale mismatch at theme translations picker (#26687) * FIX: Locale mismatch at theme translations picker Before, the theme translations picker value was set to the site's default locale, which mismatches from the user's locale. This commit changes the picker value to the user locale. relates to https://meta.discourse.org/t/locale-mismatch-at-theme-translations/302879 * DEV: Address code review feedback. - https://github.com/discourse/discourse/pull/26687#discussion_r1572516758 - https://github.com/discourse/discourse/pull/26687#discussion_r1572524059 --- .../admin-customize-themes-show.js | 6 +++- .../routes/admin-customize-themes-show.js | 1 + app/controllers/admin/themes_controller.rb | 1 + spec/system/admin_customize_themes_spec.rb | 30 ++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show.js b/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show.js index 453e8180e8b..a6c6f838419 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show.js @@ -302,7 +302,11 @@ export default class AdminCustomizeThemesShowController extends Controller { } get locale() { - return this.get("model.locale") || this.siteSettings.default_locale; + return ( + this.get("model.locale") || + this.userLocale || + this.siteSettings.default_locale + ); } @action diff --git a/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show.js b/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show.js index 55e65959173..f1377754b3a 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show.js +++ b/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show.js @@ -41,6 +41,7 @@ export default class AdminCustomizeThemesShowRoute extends Route { colorSchemes: parentController.get("model.extras.color_schemes"), editingName: false, editingThemeSetting: false, + userLocale: parentController.get("model.extras.locale"), }); this.handleHighlight(model); diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb index 3767e39db85..16c1c50729b 100644 --- a/app/controllers/admin/themes_controller.rb +++ b/app/controllers/admin/themes_controller.rb @@ -176,6 +176,7 @@ class Admin::ThemesController < Admin::AdminController themes: serialize_data(@themes, ThemeSerializer), extras: { color_schemes: serialize_data(@color_schemes, ColorSchemeSerializer), + locale: current_user.effective_locale, }, } diff --git a/spec/system/admin_customize_themes_spec.rb b/spec/system/admin_customize_themes_spec.rb index be62b4b6da9..0403aef2510 100644 --- a/spec/system/admin_customize_themes_spec.rb +++ b/spec/system/admin_customize_themes_spec.rb @@ -3,7 +3,7 @@ describe "Admin Customize Themes", type: :system do fab!(:color_scheme) fab!(:theme) - fab!(:admin) + fab!(:admin) { Fabricate(:admin, locale: "en") } let(:admin_customize_themes_page) { PageObjects::Pages::AdminCustomizeThemes.new } @@ -160,5 +160,33 @@ describe "Admin Customize Themes", type: :system do theme_translations_settings_editor.fill_in("Hello World in French") theme_translations_settings_editor.save end + + it "should match the current user locale translation" do + SiteSetting.allow_user_locale = true + SiteSetting.set_locale_from_accept_language_header = true + SiteSetting.default_locale = "fr" + + theme.set_field( + target: :translations, + name: "en", + value: { en: { group: { hello: "Hello there!" } } }.deep_stringify_keys.to_yaml, + ) + theme.set_field( + target: :translations, + name: "fr", + value: { fr: { group: { hello: "Bonjour!" } } }.deep_stringify_keys.to_yaml, + ) + theme.save! + + visit("/admin/customize/themes/#{theme.id}") + + theme_translations_settings_editor = + PageObjects::Components::AdminThemeTranslationsSettingsEditor.new + + expect(theme_translations_settings_editor.get_input_value).to have_content("Hello there!") + + theme_translations_picker = PageObjects::Components::SelectKit.new(".translation-selector") + expect(theme_translations_picker.component.text).to eq("English (US)") + end end end