There is a website on Wordpress and 2 languages ​​UA and RU. Localization is made by creating a copy of the site in the desired language on a subdomain such as mymymain.com

Task.
- Memorize the choice of localization by the user to further download the desired localization.
or
- Determine the browser language and display the desired locale, depending on this

I found solutions: https://stackoverflow.com/questions/6038236/http-accept-language

code example (found in free access. Code author - PHP Programmer [XyZ])

// действие с проверкой языка и установкой куки языка проводим только на странице index.php if (strpos($_SERVER["REQUEST_URI"], "/index.php") or (substr($_SERVER["REQUEST_URI"], -1) == "/")) { // если вход произведен без указания языка if (!$_GET['lang']) { // то проверяем, может язык указан в куках, если нет, то то смотрим локализацию браузера if (!$_COOKIE['lang']) { $b_lang = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]); // если локализация русская или украинская или белорусская, то считаем, что пользователю необходимо отдавать русскую версию сайта. if (($b_lang[0] == "ru") or ($b_lang[0] == "be") or ($b_lang[0] == "ru-UA")) $lang = "rus"; // при всех остальных локализациях отдаем английскую версию else $lang = "eng"; } // если язык уже указан в куках, то его и используем в системе else $lang = $_COOKIE['lang']; } // если страница запрошена с указанием языка, то этот язык и используем в системе, и запоминаем его в куку else { $lang = $_GET['lang']; setcookie("lang", $lang, time()+30758400, "/"); } } 

The essence of the work of the proposed options understood. But I am just starting to deal with php and I don’t know how to write the “mechanics” of loading the correct locale itself.

    1 answer 1

    PHP code to determine if there are no cookies.

     if (!isset($_COOKIE['lang'])) { //Если нет куков $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); //Определяем язык браузера if ($lang != 'ru') {$lang = "ua";} // Если не украинский - то ставим русский, по умолчанию setcookie('lang', $lang); $_COOKIE['lang'] = $lang; //Записываем в куки } 

    Further the variable $ lang is used in the redirection function. I do not know how the language is chosen on your site, but let it be two links. Then add a js handler so that the user can select a language and the browser remembers his choice.

     <a href="#" class="lang" id="ru">RUS</a> <a href="#" class="lang" id="ua">UA</a> $('a.lang').on('click', function(){ //Обрабатываем клик по выбору языка document.cookie = "lang="+$(this).attr("id"); // Записываем в куки язык }); 

    All in a simplified version. For the address, you can add a GET request to send the link directly to the desired version of the site. I can write if required.

    • language, 2 buttons / links like mydomain.com and ru.mydomain.com> I can write if required. Thanks, please write - Holmes
    • I think there is no point in processing GET requests if you already have two different domains. Just add the redirect function. - Plikard
    • header('Location: http://'. if ($lang = 'ru') {echo "ru.";} .'mydomain.com'); Something like this, just prescribe the condition whether a redirect is necessary so that it is not infinite. I think by comparing the current URL with the one to which the redirect will occur. Or check for the occurrence of a language subdomain. - Plikard
    • > just add another condition, whether a redirect is necessary, so that it would not be infinite. Unfortunately I can not. I read, I study now work of function of a redirect of php - Holmes