<?phpset_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);?>
Looks interesting.Where to get deepl api key and what are free limits?