anwaar Posted February 4 Share Posted February 4 (edited) By default, PrestaShop selects the language based on the browser language. This can be a problem when most visitors are from one country but use English browsers. Below is a simple approach to set the default language based on the visitor’s country detected from IP, instead of relying only on HTTP_ACCEPT_LANGUAGE. How it works Detect visitor IP Resolve country from IP Map country → language Set language in cookie before page rendering Example PHP code $ip = Tools::getRemoteAddr(); $response = file_get_contents( 'https://api.ipgeolocation.io/v2/ipgeo?apiKey=YOUR_API_KEY&ip=' . $ip ); if ($response !== false) { $data = json_decode($response, true); if (!empty($data['location']['country_code2'])) { $countryCode = strtolower($data['location']['country_code2']); // Example mapping: Vietnam -> Vietnamese if ($countryCode === 'vn') { $lang = Language::getLanguageByIETFCode('vi'); if ($lang && $lang->active) { Context::getContext()->cookie->id_lang = (int) $lang->id; Context::getContext()->language = $lang; } } } } You will need an API key from ipgeolocation.io to run this example. The free plan allows up to 1,000 requests per day; higher usage requires an upgrade. Notes You should cache the detected language in a cookie or session to avoid calling the API on every request. This logic can also be extended for: Currency selection Country-based shipping rules Store redirection This approach works well when browser language does not represent the user’s actual location. Edited February 4 by anwaar (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now