Website in two languages

Asked

Viewed 2,503 times

0

I will have to develop a website in two languages, Portuguese and English. Nothing complex, but I am in doubt on 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 take to the page in English, if the user is interested. For example, user is on index and goes to the contact page, the site follows the normal flow in Portuguese. If the user accesses the index and changes the language, the same index will be loaded in English and then if they click 'contact', the contact page will be loaded in English. When the language is Portuguese, at the top there will be an American flag (button) for language exchange, and vice versa.

How can I structure this? Thank you.

  • 1

    Yes it is possible to do with pure php. Go from creating one or more files with an array containing the indenters and repective values or store this in the database. There on the right is the question area relacionadas give a look also always have interesting things on the theme.

  • @rray I wish to have a similar feature to android for internationalization huh,.

  • The most robust solution is to use the gettext, this is a good tutorial: http://www.devin.com.br/php-gettext/.

  • 1

    Here’s a good tutorial for this: http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html Hj I don’t do that anymore. I prefer to use Wordpress with WPML plugin.

  • If the link indicated above does not contain an adequate answer to the question, you can [Dit] the same by adding the specifics of your problem that are different from that. Alternatively, here’s another one approach for the same problem: http://answall.com/questions/15937 - Anything, leave a comment.

1 answer

3


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

In the example, if the browser is in another language than English or Portuguese it will present the site in English,

And we use on the site, see an example:


Language files:

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 keys are always equal, only the value that should have the content in the language set.


Website page:

Here is where defined the layout of the site, first of all we get the language, 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 make a link to each language, with the action of changing the variable $idioma for pt-br or en-us.

To test the example above, run once with the Portuguese browser, after that change the language of the same and access again the index.php.

Browser other questions tagged

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