Skip to content

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

FIX: Format dates with ASCII digits in Persian and Arabic locales (#43690)

Kris82e56f500282
diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb
index 9e8d73d7016..827c63a6e1a 100644
--- a/lib/js_locale_helper.rb
+++ b/lib/js_locale_helper.rb
@@ -221,6 +221,7 @@ module JsLocaleHelper
throw new Error("globalThis.moment not defined. Failed to initialize locales.")
}
#{moment_locale(locale_str)}
+ #{moment_ascii_digits}
#{moment_locale(locale_str, timezone_names: true)}
#{moment_formats}
}
@@ -330,6 +331,16 @@ module JsLocaleHelper
"moment.fn.#{name.camelize(:lower)} = function(){ return this.format('#{format}'); };\n"
end
+ # Some locales format dates with native-script digits, which breaks dates
+ # sent in URLs, request params and input values.
+ def self.moment_ascii_digits
+ <<~JS
+ if (moment.localeData().postformat("0") !== "0") {
+ moment.updateLocale(moment.locale(), { postformat: (string) => string });
+ }
+ JS
+ end
+
def self.moment_locale(locale, timezone_names: false)
_, filename = find_moment_locale([locale], timezone_names: timezone_names)
if filename && File.exist?(filename)
diff --git a/spec/lib/js_locale_helper_spec.rb b/spec/lib/js_locale_helper_spec.rb
index 42384f87257..8d0dd8fae68 100644
--- a/spec/lib/js_locale_helper_spec.rb
+++ b/spec/lib/js_locale_helper_spec.rb
@@ -34,6 +34,18 @@ RSpec.describe JsLocaleHelper do
"wizard_js",
)
end
+
+ { "ar" => "ar", "fa_IR" => "fa" }.each do |locale, moment_locale|
+ it "formats dates with ASCII digits for #{locale}" do
+ v8_ctx.eval(File.read(Rails.root.join("frontend/discourse/node_modules/moment/moment.js")))
+ v8_ctx.eval("moment.tz = {};")
+ v8_ctx.eval(JsLocaleHelper.output_locale(locale))
+ v8_ctx.eval("window._discourse_locale_data.configureMoment();")
+
+ expect(v8_ctx.eval("moment.locale()")).to eq(moment_locale)
+ expect(v8_ctx.eval('moment("2026-05-01").format("YYYY-MM-DD")')).to eq("2026-05-01")
+ end
+ end
end
LocaleSiteSetting.values.each do |locale|