Translation of pages in php files

0

Hello, I'm developing a website that has only 4 pages, and I want to have it in at least six languages, but what should I do for it?

Even with a few pages, I need a lot of text, because the login page includes the "country" field, which contains many countries for example! The idea is as follows:

I want to create multiple files in a "language" folder, such as " portuguese.inc.php ", " spanish.inc, php ", english.inc.php

And within each file as an example:

// Account
$hello          = "Olá";
$welcome        = "Bem-Vindo";
$go_in          = "Entre";
$or             = "ou";
$sign_up        = "Cadastre-se";
$sign_in        = "Entrar";
$forgot_password= "Esqueceu a Senha?";
$logout         = "Sair";
$my_account     = "Minha Conta";

// Languages
$language       = "Idioma";
$english        = "Ingles";
$portuguese     = "Português";
$spanish        = "Espanhol";
$chinese        = "Chinês";
$italian        = "Italiano";
$german         = "Alemão";
$french         = "Francês";
$arabic         = "Arabico";
$russian        = "Russo";
$japanese       = "Japonês";

And where will I use the files:

Index.php

<?php
    include_once 'languages/portuguese.inc.php';
    // Minha Conta
    // Olá, Bem-Vindo!
    // Entre ou Cadastre-se!

    echo "$my_account";
    echo "$hello $welcome!";
    echo "$go_in $or $sign_up";

What do you think?

Is this the best alternative?

Is there any consequence to this?

    
asked by anonymous 27.07.2015 / 04:32

1 answer

3

It works without problems the way it showed, but I recommend that you use array and avoid including directly in the pages as there may be a collision of variable names.

Recommendations

1. Naming of translation files

Name the files using ISO codes. Example, instead of portugues.inc.php, use pt.inc.php (.inc is unnecessary, but not the case in question)

2. Data array return

In the file "portugues.inc.php", format the content as an array with "return". Example

<?php
return array(
'hello' => 'Olá',
'welcome' => 'Bem vindo',
);
?> 

Make include by assigning it to a variable,

$ labels = include_once 'languages / portuguese.inc.php';

To read content, echo $labels['hello'];

3. Logical Recommendation for Proper Names Translation

When translating proper names, it is recommended to keep the name in the original language.

Note An example with the current logic:

$english        = "Ingles";
$portuguese     = "Português";
$spanish        = "Espanhol";
$chinese        = "Chinês";

Assume a user who speaks Chinese and enters the page through another language, for example, Portuguese. This user will have great difficulty navigating the page or even being able to choose the language for Chinese because the labels will all be in Portuguese. Chinese have no idea what "Chinese" means or even in English "chinese". The safest way is to keep 2 labels together, or one in the original language.

Example

$english        = 'English';
$portuguese     = 'Português';
$spanish        = 'Español';
$chinese        = '中文';

The same goes for all other languages. Note that even if you do not understand any of the other languages, it is visually much more practical to find the desired language.

The second form includes 2 labels, usually the term in the original language + internationalized / global language

$english        = 'English';
$portuguese     = 'Português (portuguese)';
$spanish        = 'Español (spanish)';
$chinese        = '中文 (chinese)';

This form seems unnecessary, because for those who do not understand a language, it will not make a difference to enter the page with such language.

4. Language Array

Languages should be organized in a particular list. Example:

languages.php

return array(
'en' = 'English',
'pt' = 'Português',
'es' = 'Español',
'cn' = '中文'
);

obs: There are "N" forms and techniques. So do not take that response as definitive, as "the best solution." But surely a more organized solution than the current state.

    
27.07.2015 / 09:19