Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

feat(i18n): apply user locale and 12h/24h preferences to date formatting

Andrii Andrushchyshyn3b936fc79755
diff --git a/client/src/app/[site]/main/components/MainSection/Chart.tsx b/client/src/app/[site]/main/components/MainSection/Chart.tsx
index 96832593a..80c781247 100644
--- a/client/src/app/[site]/main/components/MainSection/Chart.tsx
+++ b/client/src/app/[site]/main/components/MainSection/Chart.tsx
@@ -9,7 +9,12 @@ import { APIResponse } from "../../../../../api/types";
import { Time } from "../../../../../components/DateSelector/types";
import { formatSecondsAsMinutesAndSeconds } from "../../../../../lib/utils";
-export const formatter = Intl.NumberFormat("en", { notation: "compact" });
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+const resolved = new Intl.DateTimeFormat(undefined, { hour: 'numeric' }).resolvedOptions();
+const is24Hour = resolved.hourCycle === 'h23' || resolved.hourCycle === 'h24';
+
+export const formatter = Intl.NumberFormat(userLocale, { notation: "compact" });
const getMax = (time: Time, bucket: TimeBucket) => {
const now = DateTime.now();
@@ -289,23 +294,11 @@ export function Chart({
: Math.min(12, data?.data?.length ?? 0)
),
format: (value) => {
- // Convert UTC date to local timezone for display
- const localTime = DateTime.fromJSDate(value).toLocal();
-
- if (time.mode === "last-24-hours" || time.mode === "day") {
- return localTime.toFormat("ha");
- } else if (time.mode === "range") {
- return localTime.toFormat("MMM d");
- } else if (time.mode === "week") {
- return localTime.toFormat("MMM d");
- } else if (time.mode === "month") {
- return localTime.toFormat("MMM d");
- } else if (time.mode === "year") {
- return localTime.toFormat("MMM d");
- } else if (time.mode === "all-time") {
- return localTime.toFormat("MMM d");
+ const dt = DateTime.fromJSDate(value).setLocale(userLocale);
+ if (time.mode === "day" || time.mode === "last-24-hours" ) {
+ return dt.toFormat(is24Hour ? 'HH:mm' : 'ha');
}
- return "";
+ return dt.toFormat(is24Hour ? 'dd MMM' : 'MMM d');
},
}}
axisLeft={{
@@ -354,12 +347,12 @@ export function Chart({
</div>
) : null}
<div className="flex justify-between text-sm w-36">
- <div>{formatTime(currentTime, bucket)}</div>
+ <div>{formatDateTime(currentTime, bucket)}</div>
<div>{formatTooltipValue(currentY, selectedStat)}</div>
</div>
{previousTime && (
<div className="flex justify-between text-sm text-muted-foreground">
- <div>{formatTime(previousTime, bucket)}</div>
+ <div>{formatDateTime(previousTime, bucket)}</div>
<div>{formatTooltipValue(previousY, selectedStat)}</div>
</div>
)}
@@ -383,20 +376,16 @@ export function Chart({
);
}
-const formatTime = (time: DateTime<boolean>, bucket: TimeBucket) => {
- // Ensure time is in local timezone
- const localTime = time.toLocal();
-
- if (
- bucket === "minute" ||
- bucket === "five_minutes" ||
- bucket === "ten_minutes" ||
- bucket === "fifteen_minutes"
- ) {
- return localTime.toFormat("M/d h:mm a");
- } else if (bucket === "hour") {
- return localTime.toFormat("M/d h a");
- } else {
- return localTime.toLocaleString();
+const formatDateTime = (dt: DateTime, bucket: TimeBucket) => {
+ const showMinutes = ["minute", "five_minutes", "ten_minutes", "fifteen_minutes", "hour"].includes(bucket);
+ const options: Intl.DateTimeFormatOptions = {
+ month: 'short',
+ day: 'numeric',
+ hour: 'numeric',
+ hour12: !is24Hour,
+ };
+ if (showMinutes && is24Hour) {
+ options.minute = 'numeric';
}
+ return new Intl.DateTimeFormat(userLocale, options).format(dt.toJSDate());
};
diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsx
index b8b737469..dd9c77d73 100644
--- a/client/src/components/Sessions/SessionCard.tsx
+++ b/client/src/components/Sessions/SessionCard.tsx
@@ -49,6 +49,11 @@ function truncatePath(path: string, maxLength: number = 32) {
return `${path.substring(0, maxLength)}...`;
}
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+const resolved = new Intl.DateTimeFormat(undefined, { hour: 'numeric' }).resolvedOptions();
+const is24Hour = resolved.hourCycle === 'h23' || resolved.hourCycle === 'h24';
+
export function SessionCard({ session, onClick, userId }: SessionCardProps) {
const [expanded, setExpanded] = useState(false);
@@ -190,9 +195,10 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
<span className="text-gray-400">
{DateTime.fromSQL(session.session_start, {
zone: "utc",
- })
+ }).setLocale(userLocale)
.toLocal()
- .toFormat("MMM d, h:mm a")}
+ .toFormat(is24Hour ? 'dd MMM, HH:mm' : 'MMM d, h:mm a')
+ }
</span>
<span className="hidden md:block">{durationFormatted}</span>
</div>
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index 190d9696b..38838318b 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -29,6 +29,11 @@ import { OperatingSystem } from "../../app/[site]/components/shared/icons/Operat
import { cn, getCountryName, getLanguageName, formatDurationHuman } from "../../lib/utils";
import { Button } from "../ui/button";
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+const resolved = new Intl.DateTimeFormat(undefined, { hour: 'numeric' }).resolvedOptions();
+const is24Hour = resolved.hourCycle === 'h23' || resolved.hourCycle === 'h24';
+
// Component to display a single pageview or event
function PageviewItem({
item,
@@ -43,7 +48,8 @@ function PageviewItem({
}) {
const isEvent = item.type !== "pageview";
const timestamp = DateTime.fromSQL(item.timestamp, { zone: "utc" }).toLocal();
- const formattedTime = timestamp.toFormat("h:mm:ss a");
+ const formattedTime = timestamp.setLocale(userLocale)
+ .toFormat(is24Hour? "HH:mm:ss" : "h:mm:ss a");
console.info(item);
diff --git a/client/src/components/SiteSessionChart.tsx b/client/src/components/SiteSessionChart.tsx
index 8389b083e..7105befe4 100644
--- a/client/src/components/SiteSessionChart.tsx
+++ b/client/src/components/SiteSessionChart.tsx
@@ -10,6 +10,11 @@ interface SiteSessionChartProps {
height?: number | string;
}
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+const resolved = new Intl.DateTimeFormat(undefined, { hour: 'numeric' }).resolvedOptions();
+const is24Hour = resolved.hourCycle === 'h23' || resolved.hourCycle === 'h24';
+
export function SiteSessionChart({
data,
height = 100,
@@ -59,7 +64,9 @@ export function SiteSessionChart({
tickRotation: 0,
tickValues: 3,
format: (value) => {
- return DateTime.fromJSDate(value).toFormat("ha");
+ return DateTime.fromJSDate(value)
+ .setLocale(userLocale)
+ .toFormat(is24Hour ? 'HH:mm' : 'ha');
},
}}
axisLeft={{
@@ -96,7 +103,10 @@ 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">
- {currentTime.toFormat("ha")}
+ {currentTime
+ .setLocale(userLocale)
+ .toFormat(is24Hour ? 'HH:mm' : 'ha')
+ }
</div>
<div className="font-medium">{currentY.toLocaleString()}</div>
</div>