Multi-language system

Asked

Viewed 1,007 times

3

I have a website and I would like to implement a multi-language system. I have some questions about good practice.
I wish I had just one domain ****.com. I think this would require 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 by clicking on the intended language it would change the global variable and whenever you open a new page of the site it would take on the new language.
This would be good practice?

2 answers

1

There are some features that you can detect the language by IP if it is the user’s first visit to your site. (This kind of service can be unstable, there are other paid ones 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 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']);
}

How to set which language the user is using is the simplest. The most complicated part will be to translate the content.

  • Thank you for your help.

1

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

I asked a question that I recommend reading:

MVC and dynamic language change

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

For those who, like me, professionally use their own framework and implement their own MVC and for various reasons are forced to find mechanisms to solve various problems. Language control is a problem to always take into account.

Get the browser language. It is a practice to adopt. This way we are already following definitions that the browser itself ensures and that delivers us by tray.

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

From that point on, I think the manipulation of a URL amigável to set a language is a simple, efficient and very direct solution in a PHP project and not only.

normal access:

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

access affecting the idol:

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 at any time can change the language.

The methodology for storing the selected language depends on your solution but saving to COOKIE can be one of them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.