i18n: Move locale code behind the feature toggle in internationalization/dates.ts (#103981)

This commit is contained in:
Laura Fernández
2025-04-16 09:54:10 +02:00
committed by GitHub
parent 73307a6f8f
commit 68c6e4b1e7
2 changed files with 14 additions and 5 deletions
+11 -4
View File
@@ -4,8 +4,12 @@ import memoize from 'micro-memoize';
import { config } from 'app/core/config';
import { getI18next } from './index';
const deepMemoize: typeof memoize = (fn) => memoize(fn, { isEqual: deepEqual });
const isLocaleEnabled = config.featureToggles.localeFormatPreference;
const createDateTimeFormatter = deepMemoize((locale: string, options: Intl.DateTimeFormatOptions) => {
return new Intl.DateTimeFormat(locale, options);
});
@@ -20,17 +24,20 @@ export const formatDate = deepMemoize(
return formatDate(new Date(value), format);
}
const locale = config.locale;
const dateFormatter = createDateTimeFormatter(locale, format);
const i18n = getI18next();
const currentLocale = isLocaleEnabled ? config.locale : i18n.language;
const dateFormatter = createDateTimeFormatter(currentLocale, format);
return dateFormatter.format(value);
}
);
export const formatDuration = deepMemoize(
(duration: Intl.DurationInput, options: Intl.DurationFormatOptions = {}): string => {
const locale = config.locale;
const i18n = getI18next();
const currentLocale = isLocaleEnabled ? config.locale : i18n.language;
const dateFormatter = createDurationFormatter(locale, options);
const dateFormatter = createDurationFormatter(currentLocale, options);
return dateFormatter.format(duration);
}
);
+3 -1
View File
@@ -23,7 +23,9 @@ const settings: Partial<GrafanaBootConfig> = {
};
global.grafanaBootData = {
settings,
user: {},
user: {
locale: 'en-US',
},
navTree: [],
};