timerender: Remove unnecessary get_user_locale function.

Subscripting an empty array does not throw an exception in JavaScript,
"default" is not a valid locale, and the browser APIs already fall
back on unrecognized locales anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-11-17 23:20:13 -08:00 committed by Tim Abbott
parent c539b8ee46
commit 100f91e1c9
2 changed files with 6 additions and 16 deletions

View File

@ -94,17 +94,6 @@ function get_format_options_for_type(type: DateOrTimeFormat): Intl.DateTimeForma
}
}
function get_user_locale(): string {
const user_default_language = user_settings.default_language;
let locale = "";
try {
locale = Intl.DateTimeFormat.supportedLocalesOf(user_default_language)[0];
} catch {
locale = "default";
}
return locale;
}
// Common function for all date/time rendering in the project. Handles
// localization using the user's configured locale and the
// twenty_four_hour_time setting.
@ -114,8 +103,10 @@ export function get_localized_date_or_time_for_format(
date: Date | number,
format: DateOrTimeFormat,
): string {
const locale = get_user_locale();
return new Intl.DateTimeFormat(locale, get_format_options_for_type(format)).format(date);
return new Intl.DateTimeFormat(
user_settings.default_language,
get_format_options_for_type(format),
).format(date);
}
// Exported for tests only.
@ -467,8 +458,7 @@ export function get_full_datetime_clarification(
time: Date,
time_format: TimeFormat = "time_sec",
): string {
const locale = get_user_locale();
const date_string = time.toLocaleDateString(locale);
const date_string = time.toLocaleDateString(user_settings.default_language);
let time_string = get_localized_date_or_time_for_format(time, time_format);
const tz_offset_str = get_tz_with_UTC_offset(time);

View File

@ -39,7 +39,7 @@ run_test("get_localized_date_or_time_for_format returns default date with incorr
const date = date_2019;
const expectedDate = "Friday, April 12, 2019";
user_settings.default_language = "invalidLanguageCode";
user_settings.default_language = "invalid";
const actualDate = timerender.get_localized_date_or_time_for_format(
date,
"weekday_dayofyear_year",