I want to put inside an existing ARRAY the information of a text file . TXT with PHP

Asked

Viewed 72 times

0

The idea is to translate a system into PHP, where each ARRAY key is a translation.

Manually I have already managed to remove all keys and translate them, separate each translated key in a row, placing all in a text file.txt, now I want to put these already translated lines inside the ARRAY as VALUE, automatically pulling from the text file.txt

Ex:

<?php
$lang["CHAVE"] ="VALOR";
?>

Part of the code is this:

<?php
$lang["Signup"]              ="";
$lang["Sign In"]             ="";
$lang['Login Successful']    ='';
?>

It has to stay that way:

<?php
$lang["Signup"]              ="Inscrição";
$lang["Sign In"]             ="Entrar";
$lang['Login Successful']    ="Login com êxito";
?:>

This file has more than 2600 lines, all in this pattern, so I want to automate this work, has more files like this to do.

I noticed that there are still many PHP scripts that use this form of translation, it would be interesting to build a script to do this automated work.

Text file.txt

Inscrição
Entrar
Login com êxito
  • What is the format of the TXT file? You can put the first lines so we know how the data is in it.

  • Plain text only, one sentence per line.

  • And how will it be possible to associate each row with a corresponding key in the array? If the line format was something like KEY=translated text, it would be possible. However, it seems that there is only the translated text. How do we know if a line belongs to the Signup key or if it belongs to Sign in, for example?

  • The idea and each line correspond to a key in sequential order EX: First record of the ARRAY $lang["Signup"] ="Inscription"; 1 line of the TXT file -> Inscription

1 answer

0

Although I understand it to be a bad logic proposed by the question, I think this solves the question:

$lang = ['Signup', 'Sign in', 'Login successfull'];

foreach ($lang as $key => $value) {
    $txt = trim(fgets($file_handle))
    $lang[$key] = $txt;
}

Note that I have omitted the opening/closing code of the text file (which can be done by fopen() and fclose().

I also assumed that the text file does not have blank lines at the beginning and middle of the translations.

  • I understand, my problem and the delay in translating systems, how would you do the translations in a faster way? Files with 4 or 5 thousand lines, today I do one line at a time. it takes me days just to translate.

  • The issue is not the translation itself, but rather you are required to follow the same order in the file with the translations as the order of the variable keys $lang. At the very least, there should be a key in the translation archive for each key in $lang. Many years ago I wrote a tool for internationalization with PHP: https://github.com/everton3x/AlText and https://github.com/everton3x/i81n Take a look at them to understand the concept.

Browser other questions tagged

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