🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
refactor: use Luxon Settings.defaultLocale globally instead of localeFormat
Andrii Andrushchyshyn59d007e6d01f
diff --git a/client/src/app/[site]/retention/RetentionChart.tsx b/client/src/app/[site]/retention/RetentionChart.tsxindex c3ae74e51..ae0797e23 100644--- a/client/src/app/[site]/retention/RetentionChart.tsx+++ b/client/src/app/[site]/retention/RetentionChart.tsx@@ -9,7 +9,6 @@ import {RetentionMode,} from "../../../api/analytics/useGetRetention";import { Skeleton } from "../../../components/ui/skeleton";-import { localeFormat } from "../../../lib/dateTimeUtils";interface RetentionChartProps {data: ProcessedRetentionData | undefined;@@ -77,18 +76,18 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {// Format the date label based on modelet formattedDate: string;+ const startDate = DateTime.fromISO(cohortKey);if (mode === "day") {- formattedDate = localeFormat(DateTime.fromISO(cohortKey), "MMM dd");+ formattedDate = startDate.toFormat("MMM dd");} else {// For weekly mode- const startDate = DateTime.fromISO(cohortKey);const endDate = startDate.plus({ days: 6 });// If same month, don't repeat monthif (startDate.month === endDate.month) {- formattedDate = `${localeFormat(startDate, "MMM dd")}-${localeFormat(endDate, "dd")}`;+ formattedDate = `${startDate.toFormat("MMM dd")}-${endDate.toFormat("dd")}`;} else {- formattedDate = `${localeFormat(startDate, "MMM dd")}-${localeFormat(endDate, "MMM dd")}`;+ formattedDate = `${startDate.toFormat("MMM dd")}-${endDate.toFormat("MMM dd")}`;}}@@ -210,19 +209,19 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {if (originalCohortKey) {const startDate = DateTime.fromISO(originalCohortKey);if (mode === "day") {- cohortDateDisplay = localeFormat(startDate, "MMM dd, yyyy");+ cohortDateDisplay = startDate.toFormat("MMM dd, yyyy");} else {const endDate = startDate.plus({ days: 6 });if (startDate.month === endDate.month) {- cohortDateDisplay = `${localeFormat(startDate, "MMM dd"- )} - ${localeFormat(endDate, "dd, yyyy")}`;+ cohortDateDisplay = `${startDate.toFormat("MMM dd"+ )} - ${endDate.toFormat("dd, yyyy")}`;} else if (startDate.year === endDate.year) {- cohortDateDisplay = `${localeFormat(startDate, "MMM dd"- )} - ${localeFormat(endDate, "MMM dd, yyyy")}`;+ cohortDateDisplay = `${startDate.toFormat("MMM dd"+ )} - ${endDate.toFormat("MMM dd, yyyy")}`;} else {- cohortDateDisplay = `${localeFormat(startDate, "MMM dd, yyyy"- )} - ${localeFormat(endDate, "MMM dd, yyyy")}`;+ cohortDateDisplay = `${startDate.toFormat("MMM dd, yyyy"+ )} - ${endDate.toFormat("MMM dd, yyyy")}`;}}}diff --git a/client/src/app/[site]/retention/page.tsx b/client/src/app/[site]/retention/page.tsxindex 77ff5fb02..ecd51f27e 100644--- a/client/src/app/[site]/retention/page.tsx+++ b/client/src/app/[site]/retention/page.tsx@@ -25,7 +25,6 @@ import { Tabs, TabsList, TabsTrigger } from "../../../components/ui/tabs";import { RetentionChart } from "./RetentionChart";import { NothingFound } from "../../../components/NothingFound";import { useSetPageTitle } from "../../../hooks/useSetPageTitle";-import { localeFormat } from "../../../lib/dateTimeUtils";// Available time range options (in days)const RANGE_OPTIONS = [@@ -99,23 +98,22 @@ export default function RetentionPage() {// Function to format date based on modeconst formatDate = (dateStr: string) => {+ const startDate = DateTime.fromISO(dateStr);if (mode === "day") {- const date = DateTime.fromISO(dateStr);- return localeFormat(date, "MMM dd, yyyy");+ return startDate.toFormat("MMM dd, yyyy");} else {// For weekly mode, show start and end dates of the week- const startDate = DateTime.fromISO(dateStr);const endDate = startDate.plus({ days: 6 }); // End of week (7 days total)// If same month, don't repeat month nameif (startDate.month === endDate.month) {- return `${localeFormat(startDate, "MMM dd")} - ${localeFormat(endDate, "dd, yyyy")}`;+ return `${startDate.toFormat("MMM dd")} - ${endDate.toFormat("dd, yyyy")}`;} else if (startDate.year === endDate.year) {// Different months, same year- return `${localeFormat(startDate, "MMM dd")} - ${localeFormat(endDate, "MMM dd, yyyy")}`;+ return `${startDate.toFormat("MMM dd")} - ${endDate.toFormat("MMM dd, yyyy")}`;} else {// Different years- return `${localeFormat(startDate, "MMM dd, yyyy")} - ${localeFormat(endDate, "MMM dd, yyyy")}`;+ return `${startDate.toFormat("MMM dd, yyyy")} - ${endDate.toFormat("MMM dd, yyyy")}`;}}};diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsxindex 687ea967d..b03424f1f 100644--- a/client/src/components/Sessions/SessionDetails.tsx+++ b/client/src/components/Sessions/SessionDetails.tsx@@ -29,7 +29,7 @@ import { OperatingSystem } from "../../app/[site]/components/shared/icons/Operatimport { cn, getCountryName, getLanguageName } from "../../lib/utils";import { formatDuration } from "../../lib/dateTimeUtils";import { Button } from "../ui/button";-import { localeFormat, is24Hour } from "../../lib/dateTimeUtils";+import { is24Hour } from "../../lib/dateTimeUtils";// Component to display a single pageview or eventfunction PageviewItem({@@ -45,7 +45,7 @@ function PageviewItem({}) {const isEvent = item.type !== "pageview";const timestamp = DateTime.fromSQL(item.timestamp, { zone: "utc" }).toLocal();- const formattedTime = localeFormat(timestamp, is24Hour ? "HH:mm:ss" : "h:mm:ss a");+ const formattedTime = timestamp.toFormat(is24Hour ? "HH:mm:ss" : "h:mm:ss a");// Calculate duration if this is a pageview and we have the next timestamplet duration = null;diff --git a/client/src/components/SiteSessionChart.tsx b/client/src/components/SiteSessionChart.tsxindex 00317aa6f..4f24d81cd 100644--- a/client/src/components/SiteSessionChart.tsx+++ b/client/src/components/SiteSessionChart.tsx@@ -4,7 +4,7 @@ import { DateTime } from "luxon";import { useMemo } from "react";import { GetOverviewBucketedResponse } from "../api/analytics/useGetOverviewBucketed";import { formatter } from "../lib/utils";-import { localeFormat, is24Hour } from "../lib/dateTimeUtils";+import { is24Hour } from "../lib/dateTimeUtils";interface SiteSessionChartProps {data: GetOverviewBucketedResponse;@@ -61,7 +61,7 @@ export function SiteSessionChart({tickValues: 3,format: (value) => {const time = DateTime.fromJSDate(value);- return localeFormat(time, is24Hour ? "HH:mm" : "ha");+ return time.toFormat(is24Hour ? "HH:mm" : "ha");},}}axisLeft={{@@ -98,7 +98,7 @@ export function SiteSessionChart({<div className="text-xs mb-1">Sessions</div><div className="flex justify-between text-xs w-20"><div className="text-muted-foreground">- { localeFormat(currentTime, is24Hour ? "HH:mm" : "ha") }+ { currentTime.toFormat(is24Hour ? "HH:mm" : "ha") }</div><div className="font-medium">{currentY.toLocaleString()}</div></div>diff --git a/client/src/lib/dateTimeUtils.ts b/client/src/lib/dateTimeUtils.tsindex 85a0f1f81..c282b4a92 100644--- a/client/src/lib/dateTimeUtils.ts+++ b/client/src/lib/dateTimeUtils.ts@@ -1,5 +1,12 @@-import { Duration, DateTime, DurationLikeObject } from "luxon";+import { Duration, DateTime, DurationLikeObject, Settings } from "luxon";++// Detect user locale+export const userLocale = typeof navigator !== "undefined" ? navigator.language : "en-US";++// Set default locale globally+Settings.defaultLocale = userLocale;+/*** Returns localized weekday names, starting from Monday.@@ -94,25 +101,6 @@ export function formatDuration(minutes: number, seconds: number): string {});}-/**- * Formats a Luxon DateTime object using a locale-aware format string.- *- * Internally sets the locale of the DateTime object to the detected user locale- * before formatting, ensuring the output reflects local conventions (e.g., month names, weekdays).- *- * @param dateTime - A Luxon DateTime instance to format.- * @param format - A Luxon-compatible format string (e.g., "yyyy-MM-dd", "HH:mm", "ccc").- * @returns The formatted date/time string according to the user's locale.- */-export function localeFormat(dateTime: DateTime, format: string): string {- return dateTime.setLocale(userLocale).toFormat(format);-}-----// Detect user locale-export const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";// Detect user 12h/24h preferenceconst resolved = new Intl.DateTimeFormat(undefined, { hour: "numeric" }).resolvedOptions();