php – Multi-language system


I have a website and would like to implement a multi-language system. I have some doubts about good practices.
I would like to have only one ****.com domain. I think this would force me to use systems implemented in php, or other languages ​​to communicate with the server, right?
My idea would be to have a global variable (I don't know if there is something like this) and when clicking on the desired language it would change the global variable and whenever I open a new page on the site it would assume the new language.
Is this a good practice?


It has some features that you can detect the language by IP, in case it is the user's first visit to your site. (This type of service can be unstable, there are other paid services that are much more accurate and reliable.)

And store the language in a Cookie . So every time the user enters your site, you get this information:

$padrao = 'pt-BR';

if (empty($_COOKIE['lang'])){
    // Receba o idioma através de um formulário $_POST['lang']
    // Ou pela URL
    // Ou busque um serviço de localização externo e baseie o idioma na localização do usuário

    $lang = $_POST['lang'] || $_GET['lang'] || buscaIdioma();

    $expire = 10 * 365 * 24 * 60 * 60; // 10 anos, ou nunca

    setcookie('lang', $lang, $expire);

    define(LANG, $lang);
} else {
    define(LANG, $_COOKIE['lang']);

Defining which language the user is using is the simplest. The trickiest part will be translating the content.

Scroll to Top