*

Rick

  • ***
  • 47 posts
Translate for free deepl api
« on: January 22, 2024, 09:30:09 PM »
Free tip to translate files.

I have excute this on xxamp. Nothing for beginners.
Need manual check after!
But your po file in same path as this script. 8)



Quote
<?php
set_time_limit(500);

require_once 'vendor/autoload.php';

use Gettext\Loader\PoLoader;
use Gettext\Generator\PoGenerator;
use Gettext\Translations;
use DeepL\Translator;

$authKey = "YOUR API KEY"; 
$translator = new Translator($authKey);

function protectPlaceholders($text) {
    $replacements = [];
   
    $protectedText = preg_replace_callback('/%[sd]|<[^>]+>/', function ($matches) use (&$replacements) {
        $placeholder = '%%' . count($replacements) . '%%';
        $replacements[$placeholder] = $matches[0];
        return $placeholder;
    }, $text);

    return [$protectedText, $replacements];
}

function restorePlaceholders($text, $replacements) {
    return str_replace(array_keys($replacements), array_values($replacements), $text);
}

function translateWithDeepL($translator, $text, $targetLang) {
    list($protectedText, $replacements) = protectPlaceholders($text);
    $result = $translator->translateText($protectedText, null, $targetLang);
    return restorePlaceholders($result->text, $replacements);
}

function translatePoFile($inputFile, $outputFile, $targetLang, $translator) {
    $loader = new PoLoader();
    $translations = $loader->loadFile($inputFile);

    foreach ($translations as $translation) {
        if ($translation->getTranslation() === '') {
            $original = $translation->getOriginal();
            if (!empty($original)) {
                $translatedText = translateWithDeepL($translator, $original, $targetLang);
                if ($translatedText) {
                    $translation->translate($translatedText);
                }
            }
        }
    }

    $generator = new PoGenerator();
    $generator->generateFile($translations, $outputFile);
}

$inputFile = 'core.po';
$outputFile = 'core1.po';
$targetLang = 'FR';

translatePoFile($inputFile, $outputFile, $targetLang, $translator);
?>

« Last Edit: January 23, 2024, 05:41:40 PM by Rick »

*

MB Themes

Re: Translate for free deepl api
« Reply #1 on: January 23, 2024, 07:11:18 AM »
Looks interesting.
Where to get deepl api key and what are free limits?
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Rick

  • ***
  • 47 posts
Re: Translate for free deepl api
« Reply #2 on: January 23, 2024, 05:36:47 PM »
Looks interesting.
Where to get deepl api key and what are free limits?

https://www.deepl.com/en/pro-api?cta=header-pro-api
500,000 character limit / month


Need credit card to verify you dont create a lot of accounts. But free.

I have update script to dont destory html tags and so on. :)
But need a littel eye after to just check small things.

Did save me 50$ to not buy Poedit pro.
« Last Edit: January 23, 2024, 06:45:00 PM by Rick »

*

MB Themes

Re: Translate for free deepl api
« Reply #3 on: January 24, 2024, 04:03:15 PM »
Ok this sounds like good idea for some free plugin instead (as those limits, pricing and restrictions usually change over time).
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

MB Themes

  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots