Last active
November 6, 2019 20:31
-
-
Save Burick/433897c50c14922d500ef4d3085353f5 to your computer and use it in GitHub Desktop.
Автоопределение языка пользователя MODX Revo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // Определяем язык пользователя | |
$contextLang = $modx->context->config['cultureKey']; // Код контекста | |
$selectLang = isset($_COOKIE['selectLang']) ? $_COOKIE['selectLang'] : false; // Изменялся ли язык (клик по флагу)? | |
$changeLang = isset($_GET['lang']) ? $_GET['lang'] : false; // Если в адресной строке есть есть параметр GET['lang'] - пользователь хочет изменить язык сайта | |
if(!$changeLang) | |
{ | |
if($contextLang == 'ru' && (($selectLang == 'en' && $_SERVER['REQUEST_URI'] == '/ru/') || ($selectLang == false && $browserLang == 'en' && $_SERVER['REQUEST_URI'] == '/ru/'))) | |
{ | |
header('Location: http://new.granart.com.ua/'); | |
} | |
if($contextLang == 'en' &&(($selectLang == 'ru' && $_SERVER['REQUEST_URI'] == '/') || ($selectLang == false && $browserLang == 'ru' && $_SERVER['REQUEST_URI'] == '/'))) | |
{ | |
header('Location: http://new.granart.com.ua/ru/'); | |
} | |
} | |
if($changeLang == $contextLang) | |
setcookie("selectLang", $changeLang, time() + 3600 * 24 * 10, "/"); // Настроить куки можно в этом месте. | |
?> | |
====================================== | |
<?php | |
$lang = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'],","); | |
while ($lang) | |
{ | |
//check if the language is dutch | |
if (strstr($lang,"nl")) | |
{ | |
//redirect the user to the dutch pages | |
header ("location: ".$modx->makeUrl(1, '', '', 'full')); | |
exit; | |
} | |
//check if the language is english | |
if (strstr($lang,"en")) | |
{ | |
//redirect the user to the english pages | |
header ("location: ".$modx->makeUrl(1, '', '', 'full')); | |
exit; | |
} | |
//check if the language is Frans | |
if (strstr($lang,"fr")) | |
{ | |
//redirect the user to the frans pages | |
header ("location: ".$modx->makeUrl(1, '', '', 'full')); | |
exit; | |
} | |
//check if the language is deens | |
$lang = strtok(","); | |
} | |
// no defined language found, just go to the english pages | |
header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full')); | |
exit; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment