Stichoza / google-translate-php

🔤 Free Google Translate API PHP Package. Translates totally free of charge.
MIT License
1.8k stars 380 forks source link

PHP - extract the children from this <div id="page-container"> then translate it and put them back into this same <div id="page-container"> #207

Open richardsonoge opened 5 months ago

richardsonoge commented 5 months ago

This code allowed me to extract all the children from this and translate it for free using Stichoza/google-translate-php's library. However, when I put it back into this , it doesn't give the same results. The HTML content is not preserved, it has lost all its style and without exception. Secondly, accents are also not supported in the French translation. Acute accents or other accents in French are not displayed correctly as "é" is displayed this way "é". Please, do something for me...

Here's part of my code, how I tried to translate my HTML:

$htmlContent = file_get_contents('fss4.html');
$getContent = getPageContainerContent($htmlContent);

$stringSplit = str_split($getContent, 3700);
$countStringSplit = count($stringSplit);

$translateHTML = '';

for ($i = 0; $i < $countStringSplit; $i++) {
    // Translate each chunk of html
    $translateHTML .= $tr->translate($stringSplit[$i]);
}

$getCorrectTranslateHtml = preg_replace('/\/\s/', '/', $translateHTML);

$getCorrectTranslateHtml = removeSpacesBetweenTags($getCorrectTranslateHtml);

$new_html = replace_page_container($htmlContent, $getCorrectTranslateHtml);
$new_html = html_entity_decode($new_html);
$new_html = iconv('utf-8', 'latin1', $new_html);
file_put_contents('fss4_translated.html', $new_html);

Can you help me get the same results as in the original HTML file without losing the format of my HTML and fix the accent problems for me too?

I think it's because the tags are missing or Google has changed the tag format. I uploaded my code and the HTML file of this project on Github, you could see all the codes so you can help me to solve this big problem please. Any help to solve it....

I hope my question is clear and if you find it not clear enough let me know in the comments.