I'm trying this script to detect browser language and redirect to italian store view if language is italian or english store view if language is not italian. The country code is correctly detected but I'm not able to redirect to the store view. I've also tried using header('Location: ' . $lang), lang was 'italian' or 'default'. It works but I don't want to have in the url the store view code.
Here is the script:
function getCountryCode(){
$CountryCode = substr(getenv('HTTP_ACCEPT_LANGUAGE'), 0, 2);
if ($CountryCode=='it') {
$lang = 3;
}
else{
$lang = 1;
}
return $lang;
}
$lang = getCountryCode();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('\Magento\Store\Model\StoreManagerInterface')-
>setCurrentStore(0);