Two-language site [duplicate]

0

I will have to develop a site in two languages, Portuguese and English. Nothing complex, but I am in doubt as to how to organize the structure, so I would like the opinion of someone who has already been in my current situation.

The site should open in Portuguese and at the top of each page will have an American flag that will lead to the page in English, if the user has an interest. For example, the user is in the index and goes to the contact page, the site follows the normal flow in Portuguese. If the user accesses the index and switches the language, the same index will be loaded in English and then clicking 'contact' will load the contact page in English. When the language is Portuguese, at the top there will be an American flag (button) to change the language, and vice versa.

How can I structure this? Thank you.

    
asked by anonymous 28.01.2016 / 18:20

1 answer

2

The simplest way I found was by using HTTP_ACCEPT_LANGUAGE that will get the languages of browser , the first being the main one of the user. After that we retrieved the translations saved in several language files (one or more for each language).

In the example, if the browser is in a language other than English or Portuguese, it will display the site in English,

And we use the site, see an example:

Language files:

They are defined by a set of keys and values:

chave = 'valor'
chave = 'valor'

en.lang:

welcome = 'Bem-Vindo'
bye = 'Tchau.'
lng = 'Idioma:'

en-us.lang:

welcome = 'Welcome'
bye = 'Bye, Bye'
lng = 'Language:'

Note that the keys are always the same, only the value that the content in the defined language should have.

Site page:

Here is where we defined the layout of the site, before we get the language, for example:

index.html:

<?php

$idioma = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);

if(file_exists(strtolower($idioma[0]).'.lang'))
{
    $lng = parse_ini_file(strtolower($idioma[0]).'.lang');
}
else
{
    $lng = parse_ini_file('en-us.lang');
}

echo <<<SAUDACAO
<table>
    <tr>
        <td colspan="2">
        <p>{$lng['lng']} {$idioma[0]}</p>
        </td>

    </tr>
    <tr>
        <td>
        <p>{$lng['welcome']}</p>
        </td>

        <td>
        <p>{$lng['bye']}</p>
        </td>
    </tr>
</table>
SAUDACAO;

?>

If you want the user to select the language, just link to each language with the action of changing the variable $idioma to pt-br or en-us .

To test the above example, run the browser once in Portuguese, after that change its language and go back to index.php .

    
28.01.2016 / 20:09