From 4ebbdc043ecbc8f8517ab48a0ab1ad7e783a1d64 Mon Sep 17 00:00:00 2001 From: PangBo <51732678+pangbo13@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:16:09 +0800 Subject: [PATCH] fix: locale handling in assistant.rb (#705) --- lib/ai_helper/assistant.rb | 4 +++- spec/lib/modules/ai_helper/assistant_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index 99af0d3d..aa360a7e 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -29,7 +29,9 @@ module DiscourseAi prompts.map do |prompt| if prompt.name == "translate" locale = user.effective_locale - locale_hash = LocaleSiteSetting.language_names[locale] + locale_hash = + LocaleSiteSetting.language_names[locale] || + LocaleSiteSetting.language_names[locale.split("_")[0]] translation = I18n.t( "discourse_ai.ai_helper.prompts.translate", diff --git a/spec/lib/modules/ai_helper/assistant_spec.rb b/spec/lib/modules/ai_helper/assistant_spec.rb index fd3f479c..83233116 100644 --- a/spec/lib/modules/ai_helper/assistant_spec.rb +++ b/spec/lib/modules/ai_helper/assistant_spec.rb @@ -59,6 +59,11 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do ) end + it "does not raise an error when effective_locale does not exactly match keys in LocaleSiteSetting" do + SiteSetting.default_locale = "zh_CN" + expect { subject.available_prompts(user) }.not_to raise_error + end + context "when illustrate post model is enabled" do before do SiteSetting.ai_helper_illustrate_post_model = "stable_diffusion_xl"