Files
Luminessa Starlight 09e8484bac i18n: rename locale to regionalFormat (#106585)
* locale -> regionalFormat, mirroring #102233

* set up regionalFormat to replace locale entirely

* replace locale with regionalFormat

* update reportInteraction arguments
2025-06-16 21:53:55 +02:00

40 lines
1.4 KiB
TypeScript

import deepEqual from 'fast-deep-equal';
import memoize from 'micro-memoize';
import { getLanguage } from '@grafana/i18n/internal';
import { config } from 'app/core/config';
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);
});
const createDurationFormatter = deepMemoize((locale: string, options: Intl.DurationFormatOptions) => {
return new Intl.DurationFormat(locale, options);
});
export const formatDate = deepMemoize(
(value: number | Date | string, format: Intl.DateTimeFormatOptions = {}): string => {
if (typeof value === 'string') {
return formatDate(new Date(value), format);
}
const currentLocale = isLocaleEnabled ? config.regionalFormat : getLanguage();
const dateFormatter = createDateTimeFormatter(currentLocale, format);
return dateFormatter.format(value);
}
);
export const formatDuration = deepMemoize(
(duration: Intl.DurationInput, options: Intl.DurationFormatOptions = {}): string => {
const currentLocale = isLocaleEnabled ? config.regionalFormat : getLanguage();
const dateFormatter = createDurationFormatter(currentLocale, options);
return dateFormatter.format(duration);
}
);