diff --git a/src/lib/geo-region.ts b/src/lib/geo-region.ts index ef9992e..7948f3f 100644 --- a/src/lib/geo-region.ts +++ b/src/lib/geo-region.ts @@ -98,16 +98,18 @@ export function getUserGeoRegion(force = false): Promise { try { // 1. Primary: Habib Backend User Region API (/account/auth/user/region/) try { + console.log("[GEO_AUTO_LOG] 🚀 Requesting /account/auth/user/region/ from backend..."); const response = await http.get<{ ip?: string; country?: string; country_code?: string; city?: string; }>("/account/auth/user/region/", { - timeout: 3000, + timeout: 4000, }); const data = response.data; + console.log("[GEO_AUTO_LOG] 📥 Backend Raw Response:", JSON.stringify(data)); if (data && (data.country || data.country_code || data.city)) { const isoCode = data.country_code || @@ -117,6 +119,13 @@ export function getUserGeoRegion(force = false): Promise { const phoneCode = resolvePhoneCodeFromCountryCode(isoCode); const countryName = resolveCountryName(data.country || isoCode, "en") || data.country; + const countryFa = + resolveCountryName(data.country || isoCode, "fa") || countryName; + + console.log( + `[GEO_AUTO_LOG] 🌍 Resolved Country: FA=${countryFa} | EN=${countryName} | Code=${isoCode} | City=${data.city || "None"} | IP=${data.ip || "None"}`, + ); + const region: UserGeoRegion = { ip: data.ip, city: data.city, @@ -127,8 +136,11 @@ export function getUserGeoRegion(force = false): Promise { setStoredUserGeoRegion(region); return region; } - } catch { - // Fallback to secondary geo endpoints + } catch (err: any) { + console.error( + "[GEO_AUTO_LOG] ❌ Backend Region Error:", + err?.response?.data || err?.message || err, + ); } // 2. Secondary fallback: ipapi.co with 2s timeout diff --git a/src/translations/provider.tsx b/src/translations/provider.tsx index f1dad0b..aa6bd1b 100644 --- a/src/translations/provider.tsx +++ b/src/translations/provider.tsx @@ -1,8 +1,9 @@ "use client"; -import { createContext, type ReactNode, useContext, useMemo } from "react"; +import { createContext, type ReactNode, useContext, useEffect, useMemo } from "react"; import { defaultLocale, type Locale } from "@/translations/config"; import { type Dictionary, getDictionary } from "@/translations/dictionaries"; +import { getUserGeoRegion } from "@/lib/geo-region"; type I18nContextValue = { locale: Locale; @@ -20,6 +21,10 @@ type I18nProviderProps = { }; export function I18nProvider({ children, locale }: I18nProviderProps) { + useEffect(() => { + void getUserGeoRegion(true); + }, []); + const value = useMemo( () => ({ locale,