mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 14:52:46 +08:00

* locale -> regionalFormat, mirroring #102233 * set up regionalFormat to replace locale entirely * replace locale with regionalFormat * update reportInteraction arguments
40 lines
1.4 KiB
TypeScript
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);
|
|
}
|
|
);
|