Skip to content

Commit 324cbf9

Browse files
Frontend/i18n: Honor locale when searching places (#8727)
1 parent 3eb2e40 commit 324cbf9

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

app/web/utils/hooks.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
useRef,
1212
useState,
1313
} from "react";
14+
import { useTranslation } from "react-i18next";
1415
import { service } from "service";
1516
import {
1617
filterDuplicatePlaces,
@@ -85,6 +86,9 @@ const NOMINATIM_URL = process.env.NEXT_PUBLIC_NOMINATIM_URL;
8586

8687
const useGeocodeQuery = () => {
8788
const isMounted = useIsMounted();
89+
const {
90+
i18n: { languages: locales },
91+
} = useTranslation();
8892
const [isLoading, setIsLoading] = useSafeState(isMounted, false);
8993
const [error, setError] = useSafeState<string | undefined>(
9094
isMounted,
@@ -103,9 +107,16 @@ const useGeocodeQuery = () => {
103107
setIsLoading(true);
104108
setError(undefined);
105109
setResults(undefined);
106-
const url = `${NOMINATIM_URL!}search?format=jsonv2&q=${encodeURIComponent(
107-
value,
108-
)}&addressdetails=1`;
110+
111+
// Refer to https://nominatim.org/release-docs/latest/api/Search/
112+
const queryArgs = new URLSearchParams({
113+
format: "jsonv2",
114+
q: value,
115+
addressdetails: "1", // include a breakdown of the address into elements
116+
"accept-language": locales.join(","),
117+
});
118+
119+
const url = `${NOMINATIM_URL!}search?${queryArgs}`;
109120
const fetchOptions = {
110121
headers: {
111122
Accept: "application/json",
@@ -160,7 +171,7 @@ const useGeocodeQuery = () => {
160171
}
161172
setIsLoading(false);
162173
},
163-
[setError, setIsLoading, setResults],
174+
[locales, setError, setIsLoading, setResults],
164175
);
165176

166177
return { isLoading, error, results, query };

0 commit comments

Comments
 (0)