S
S
Smi_ed2019-01-26 00:38:39
PHP
Smi_ed, 2019-01-26 00:38:39

How to make a site multilingual?

What needs to be fixed to make the code work?
The main point is that the language on the site is set automatically, but it is possible to change it manually
if (isset($_SESSION['lang'])) {
if ($_SESSION['lang'] != "") {
preg_match('/^\ w{2}/',$_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
switch (strtolower($matches[0])){
case "ru": $accept_lang="ru";break;
case "en": case "uk": case "us": $accept_lang="en";break;
default: $accept_lang="en";break;
}
require_once './lang/' . $accept_lang . '.php';
if ($_SESSION['lang'] == "ru"
) { require_once './lang/ru.php';
} else if ($_SESSION['lang'] == "en") {
require_once './lang/en.php';
}
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitsliputsli, 2019-01-28
@Smi_ed

Haven't found any ready-made solutions?
In any case, this piece will work better in this form:

if (empty($_SESSION['lang'])) {
  preg_match('/^\w{2}/',$_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
  switch (strtolower($matches[0])){
    case "ru": 
      $_SESSION['lang']="ru";
      break;
    case "en": 
    case "uk": 
    case "us": 
    default: 
      $_SESSION['lang']="en";
      break;
  }
}
require_once './lang/' . $_SESSION['lang'] . '.php';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question