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.
.png.022b5452a8f28f552bc9430097a16da2.png)