Skip to content

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

FIX: Make credit limit reset time translatable (#37226)

Régis Hanol4857b08d6a7b
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
index cadb0cf4357..a462669581e 100644
--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb
+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
@@ -134,12 +134,12 @@ class LlmCreditAllocation < ActiveRecord::Base
def formatted_reset_time
return "" if next_reset_at.nil?
- next_reset_at.strftime("%l:%M%P on %b %d, %Y").strip
+ I18n.l(next_reset_at, format: :long)
end
def relative_reset_time
return "" if next_reset_at.nil?
- "in " + AgeWords.distance_of_time_in_words(Time.current, next_reset_at)
+ AgeWords.distance_of_time_in_words(Time.current, next_reset_at)
end
private
diff --git a/plugins/discourse-ai/app/views/discourse_ai/ai_bot/shared_ai_conversations/show.html.erb b/plugins/discourse-ai/app/views/discourse_ai/ai_bot/shared_ai_conversations/show.html.erb
index 6b3273efcab..224e4c64a34 100644
--- a/plugins/discourse-ai/app/views/discourse_ai/ai_bot/shared_ai_conversations/show.html.erb
+++ b/plugins/discourse-ai/app/views/discourse_ai/ai_bot/shared_ai_conversations/show.html.erb
@@ -36,7 +36,7 @@
<section>
<header class="page-header">
<h1><%= @shared_conversation.title %></h1>
- <time datetime="<%= @shared_conversation.populated_context.first.created_at.iso8601 %>"><%= @shared_conversation.populated_context.first.created_at.strftime('%d %B %Y') %></time>
+ <time datetime="<%= @shared_conversation.populated_context.first.created_at.iso8601 %>"><%= I18n.l(@shared_conversation.populated_context.first.created_at, format: :date_only) %></time>
</header>
<% @shared_conversation.populated_context.each do |post| %>
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 249b016ff90..7a9931f87b5 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -388,7 +388,7 @@ module DiscourseAi
)
end
- reset_time = e.allocation&.relative_reset_time || ""
+ reset_time = e.allocation&.formatted_reset_time || ""
locale_key = message.user.admin? ? "limit_exceeded_admin" : "limit_exceeded_user"
error_message =
I18n.t("discourse_ai.llm_credit_allocation.#{locale_key}", reset_time: reset_time)
@@ -638,7 +638,7 @@ module DiscourseAi
reply_post
rescue LlmCreditAllocation::CreditLimitExceeded => e
- reset_time = e.allocation&.relative_reset_time || ""
+ reset_time = e.allocation&.formatted_reset_time || ""
locale_key = post.user.admin? ? "limit_exceeded_admin" : "limit_exceeded_user"
error_message =
I18n.t("discourse_ai.llm_credit_allocation.#{locale_key}", reset_time: reset_time)
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index ccd56f0ac7d..88dc0ab5763 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
@@ -484,7 +484,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
expected_message =
I18n.t(
"discourse_ai.llm_credit_allocation.limit_exceeded_user",
- reset_time: allocation.relative_reset_time,
+ reset_time: allocation.formatted_reset_time,
).gsub(%r{<a\s+href=['"]([^'"]+)['"][^>]*>([^<]+)</a>}i, '[\2](\1)')
expect(last_message.message).to eq(expected_message)
expect(last_message.user_id).to eq(persona.user_id)
@@ -538,7 +538,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
expected_message =
I18n.t(
"discourse_ai.llm_credit_allocation.limit_exceeded_admin",
- reset_time: allocation.relative_reset_time,
+ reset_time: allocation.formatted_reset_time,
).gsub(%r{<a\s+href=['"]([^'"]+)['"][^>]*>([^<]+)</a>}i, '[\2](\1)')
expect(last_message.message).to eq(expected_message)
expect(last_message.user_id).to eq(persona.user_id)
@@ -1172,7 +1172,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
expected_message =
I18n.t(
"discourse_ai.llm_credit_allocation.limit_exceeded_user",
- reset_time: allocation.relative_reset_time,
+ reset_time: allocation.formatted_reset_time,
)
expect(last_post.raw).to include(expected_message)
expect(last_post.user_id).to eq(bot_user.id)
@@ -1204,7 +1204,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
expected_message =
I18n.t(
"discourse_ai.llm_credit_allocation.limit_exceeded_admin",
- reset_time: allocation.relative_reset_time,
+ reset_time: allocation.formatted_reset_time,
)
expect(last_post.raw).to include(expected_message)
expect(last_post.user_id).to eq(bot_user.id)
diff --git a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
index d553455d025..698ab52e6a9 100644
--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb
@@ -352,7 +352,7 @@ RSpec.describe LlmCreditAllocation do
allocation = Fabricate(:llm_credit_allocation)
formatted = allocation.formatted_reset_time
- expect(formatted).to match(/\d{1,2}:\d{2}[ap]m on \w+ \d{1,2}, \d{4}/)
+ expect(formatted).to eq(I18n.l(allocation.next_reset_at, format: :long))
end
it "returns empty string when next_reset_at is nil" do
@@ -369,7 +369,7 @@ RSpec.describe LlmCreditAllocation do
allocation = Fabricate(:llm_credit_allocation)
relative = allocation.relative_reset_time
- expect(relative).to match(/in .+/)
+ expect(relative).to be_present
end
it "returns empty string when next_reset_at is nil" do
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js
index cba91ac52ba..6107fa6ae43 100644
--- a/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js
@@ -147,7 +147,7 @@ module("Unit | Utility | ai-errors", function (hooks) {
const payload = {
error_type: "credit_limit_exceeded",
details: {
- reset_time_relative: "in 2 hours",
+ reset_time_relative: "2h",
},
};
@@ -158,7 +158,7 @@ module("Unit | Utility | ai-errors", function (hooks) {
// Convert htmlSafe string to regular string for testing
const messageStr = callArgs.message.toString();
assert.true(
- messageStr.includes("in 2 hours"),
+ messageStr.includes("2h"),
"Message should include relative time"
);