Jump to content

Edit History

anwaar

anwaar

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.

anwaar

anwaar

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;
            }
        }
    }
}

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.

anwaar

anwaar

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
 

<?php

use Context;
use Tools;
use Language;

// Get visitor IP
$ip = Tools::getRemoteAddr();

// Call IP geolocation API
$response = file_get_contents(
    'https://api.ipgeolocation.io/v2/ipgeo?apiKey=YOUR_API_KEY&ip=' . $ip
);

$data = json_decode($response, true);

if (!empty($data['country_code2'])) {
    $countryCode = strtolower($data['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;
        }
    }
}

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.

anwaar

anwaar

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
 

<?php

use Context;
use Tools;
use Language;

// Get visitor IP
$ip = Tools::getRemoteAddr();

// Call IP geolocation API
$response = file_get_contents(
    'https://api.ipgeolocation.io/v2/ipgeo?apiKey=YOUR_API_KEY&ip=' . $ip
);

$data = json_decode($response, true);

if (!empty($data['country_code2'])) {
    $countryCode = strtolower($data['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;
        }
    }
}

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.

×
×
  • Create New...