Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): show the scale in miles where people read miles

Elian Doranf54f6f69a52e
diff --git a/apps/client/src/utils/formatters.spec.ts b/apps/client/src/utils/formatters.spec.ts
index 4883a9864c..4f9501cec4 100644
--- a/apps/client/src/utils/formatters.spec.ts
+++ b/apps/client/src/utils/formatters.spec.ts
@@ -10,7 +10,7 @@ vi.mock("../services/i18n", () => ({
import { LOCALES } from "@triliumnext/commons";
import options from "../services/options";
-import { formatDateNumeric, formatDateTime, formatDuration, normalizeLocale } from "./formatters";
+import { formatDateNumeric, formatDateTime, formatDuration, getMeasurementSystem, normalizeLocale } from "./formatters";
describe("formatters", () => {
it("tolerates incorrect locale", () => {
@@ -153,6 +153,65 @@ describe("formatters", () => {
});
});
+ describe("getMeasurementSystem", () => {
+ it("picks miles only for the regions that state road distances in them", () => {
+ // Trilium lists plain "en" as "English (United States)", so maximizing it yields US;
+ // "en-GB" is a separate, equally imperial entry.
+ for (const locale of [ "en", "en-GB", "en_US" ]) {
+ options.set("formattingLocale", locale);
+ expect(getMeasurementSystem(), locale).toBe("imperial");
+ }
+
+ // Including the region-less and the underscore/alias forms the options page offers.
+ for (const locale of [ "de", "fr", "ro", "es", "cn", "tw", "pt_br", "ja" ]) {
+ options.set("formattingLocale", locale);
+ expect(getMeasurementSystem(), locale).toBe("metric");
+ }
+ });
+
+ it("prefers the browser locale over the UI language when set to auto-detect", () => {
+ // "Auto" formatting locale plus an English UI: an Australian reads kilometres, so the
+ // UI language must not be what decides it.
+ options.set("formattingLocale", "");
+ options.set("locale", "en");
+
+ withBrowserLocale("en-AU", () => expect(getMeasurementSystem()).toBe("metric"));
+ withBrowserLocale("en-US", () => expect(getMeasurementSystem()).toBe("imperial"));
+
+ // An explicitly chosen formatting locale still wins over the browser.
+ options.set("formattingLocale", "de");
+ withBrowserLocale("en-US", () => expect(getMeasurementSystem()).toBe("metric"));
+ });
+
+ it("falls back to the browser locale when the configured one is unusable", () => {
+ // The dev-only "en_rtl" normalizes to "en-rtl", which Intl rejects outright.
+ options.set("formattingLocale", "en_rtl");
+
+ withBrowserLocale("en-US", () => expect(getMeasurementSystem()).toBe("imperial"));
+ withBrowserLocale("fr-FR", () => expect(getMeasurementSystem()).toBe("metric"));
+ // No usable locale anywhere, so nothing suggests miles.
+ options.set("locale", "!!!invalid!!!");
+ withBrowserLocale("!!!invalid!!!", () => expect(getMeasurementSystem()).toBe("metric"));
+ options.set("locale", "en");
+ });
+
+ function withBrowserLocale(language: string, assert: () => void) {
+ const original = Object.getOwnPropertyDescriptor(navigator, "language");
+ try {
+ Object.defineProperty(navigator, "language", { value: language, configurable: true });
+ assert();
+ } finally {
+ // Absent descriptor means the getter lives on the prototype, so drop the own
+ // property added above and let it shine through again.
+ if (original) {
+ Object.defineProperty(navigator, "language", original);
+ } else {
+ Reflect.deleteProperty(navigator, "language");
+ }
+ }
+ }
+ });
+
describe("formatDuration", () => {
it("reports the value in the unit the user picked, for every time scale", () => {
expect(formatDuration(30, 1)).toBe("time_interval.seconds|30");
diff --git a/apps/client/src/utils/formatters.ts b/apps/client/src/utils/formatters.ts
index 99d748bd20..ad70d1a899 100644
--- a/apps/client/src/utils/formatters.ts
+++ b/apps/client/src/utils/formatters.ts
@@ -171,3 +171,39 @@ export function normalizeLocale(locale: string) {
default: return locale;
}
}
+
+/**
+ * Whether distances should be shown in miles or in kilometres, for consumers such as the geo map's
+ * scale bar.
+ *
+ * `Intl` exposes no measurement system (the locale-info proposal dropped it), so the locale's region
+ * is matched against the short list of countries that still state road distances in miles. A locale
+ * carrying no region is maximized first, which is what makes Trilium's plain `en` -- listed as
+ * "English (United States)" -- resolve to `US`; `en-GB` is a separate entry and resolves to `GB`.
+ */
+export function getMeasurementSystem(): "metric" | "imperial" {
+ // An explicitly chosen formatting locale wins. Failing that the browser's locale is preferred
+ // over what getFormattingLocale() would settle on (the UI language), because the UI language
+ // says nothing about where the user is -- an English UI is common well outside the US.
+ for (const candidate of [ options.get("formattingLocale"), navigator.language, getFormattingLocale() ]) {
+ const region = candidate && getRegion(candidate);
+ if (region) {
+ return IMPERIAL_REGIONS.has(region) ? "imperial" : "metric";
+ }
+ }
+
+ return "metric";
+}
+
+/** Regions that state road distances in miles rather than kilometres. */
+const IMPERIAL_REGIONS = new Set([ "US", "GB", "LR", "MM" ]);
+
+function getRegion(locale: string) {
+ try {
+ return new Intl.Locale(normalizeLocale(locale)).maximize().region;
+ } catch (e) {
+ // An unusable locale (e.g. the dev-only "en_rtl") makes the constructor throw; the caller
+ // then falls through to the browser's own locale.
+ return undefined;
+ }
+}
diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsx
index e6230f44f8..faff4c005c 100644
--- a/apps/client/src/widgets/collections/geomap/map.tsx
+++ b/apps/client/src/widgets/collections/geomap/map.tsx
@@ -4,7 +4,8 @@ import { Map as MapLibreGLMap, MapMouseEvent, NavigationControl, type Point, Sca
import { ComponentChildren, createContext, RefObject } from "preact";
import { useEffect, useImperativeHandle, useState } from "preact/hooks";
-import { useElementSize, useSyncedRef } from "../../react/hooks";
+import { getMeasurementSystem } from "../../../utils/formatters";
+import { useElementSize, useSyncedRef, useTriliumOption } from "../../react/hooks";
import { type MapLayer } from "./map_layer";
export interface GeoMouseEvent {
@@ -168,13 +169,15 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chi
return () => { map.off("click", handler); };
}, [ map, onClick ]);
- // Scale
+ // Scale. Miles or kilometres follow the formatting locale, which the option is read for: the
+ // unit is resolved inside the effect, so the control has to be rebuilt when that option changes.
+ const [ formattingLocale ] = useTriliumOption("formattingLocale");
useEffect(() => {
if (!scale || !map) return;
- const scaleControl = new ScaleControl();
+ const scaleControl = new ScaleControl({ unit: getMeasurementSystem() });
map.addControl(scaleControl);
return () => { map.removeControl(scaleControl); };
- }, [ map, scale ]);
+ }, [ map, scale, formattingLocale ]);
// Adapt to container size changes.
const size = useElementSize(containerRef);