Multi-language system

3

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 domain ****.com . 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 do not know if there is something like this) and loading in the intended language would change the global variable and whenever I opened a new page of the site it would assume the new language. Is this a good practice?

    
asked by anonymous 25.06.2015 / 22:17

2 answers

1

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

And store the language in a Cookie . So every time the user logs on to 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 http://dominio.com/?lang=pt-BR
    // 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.

    
25.06.2015 / 22:41
1

Your question is relevant and the same, for me, is more about form and not about how.

I put a question that I recommend reading:

MVC and change language dynamics

I can speak from experience that we should use a framework that implements an MVC for any of our PHP projects. I will not put the reasons here because it goes beyond the scope of the question, but it is certain that some already give some logic to control languages.

For those like me who professionally use their own framework and implement an MVC of their own and for various reasons are forced to find mechanisms to solve various problems. Language control is a problem to always be aware of.

Get the browser language. It is a practice to adopt. In this way we are already following the definitions that the own browser with own mechanisms ensures and that it delivers us of tray.

This may not be enough for some projects, however, and again from experience, a functional language change is desired while using it, or because someone locks access to the settings of a browser for example. But by default getting the language from the browser is clearly the way to go.

From this point, I think that manipulating a URL amigável to set a language is a simple, effective and very straightforward solution in a PHP project and beyond.

normal access:

www.meuprojecto.com/[controlador]/[acção]/[parametros] 
obtem as configurações do browser

access that affects the language:

www.meuprojeto.com/pt/[controlador]/[acção]/[parametros]
força o idioma a ser pt "por exemplo"

With this approach the links are spread throughout the project and you can change the language at any time.

The methodology for saving the selected language depends on your solution but COOKIE recording may be one of them.

    
28.06.2015 / 16:02