Translation of pages into php files

Asked

Viewed 974 times

0

Hello, I am developing a site 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 field "country", which contains many countries for example! The idea is this:

I intend to create several files in a "language" folder, such as, "portuguese.inc.php", "Spanish.inc,php", "english.inc.php"....

And inside 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?

This is the best alternative?

Is there any consequence to it?

1 answer

3


It works smoothly as shown, but I recommend that you use array and avoid including directly in the pages because there may be collision of variable names.

Recommendations

1. Nomenclature of translation archives

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

2. Return of data array

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

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

Do include by assigning it to a variable,

$Labels = include_once 'Languages/English.inc.php';

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

3. Logical recommendation for translation of given names

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

Note An example with current logic:

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

Suppose a user speaks Chinese and enters the page through another language, for example Portuguese. This user will have enormous difficulties navigating the page or even being able to choose the language for Chinese because the Abels will all be in Portuguese. Chinese have no idea what "Chinese" or even "Chinese" means. The safest is to keep 2 Labels together, or a single 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        = '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. Array of languages

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. Therefore, do not take this answer as something definitive, as "the best solution". But surely a more organized solution than the current state.

  • In case the translation will only occur in fixed words on the site? How to translate in case a description of a real estate site?

  • Yes, the above example is specific and more advisable for "fixed words", ie the Abels. Also called static content. But it can also be used for dynamic texts. It depends a lot on system data structure and requirements.

Browser other questions tagged

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