$text, 'langpair' => 'de|en', ]); $ctx = stream_context_create([ 'http' => [ 'method' => 'POST', 'timeout' => 60, 'header' => "Content-Type: application/x-www-form-urlencoded\r\nUser-Agent: B2inCmsBuild/1.0\r\n", 'content' => $body, ], ]); $raw = @file_get_contents('https://api.mymemory.translated.net/get', false, $ctx); if ($raw === false) { return $text; } $data = json_decode($raw, true); $out = $data['responseData']['translatedText'] ?? null; if (! is_string($out) || $out === '') { return $text; } if (($data['responseStatus'] ?? 200) !== 200) { return $text; } $cache[$text] = $out; usleep(150000); return $out; } function walk(mixed $v, callable $fn): mixed { if (is_string($v)) { return $fn($v); } if (is_array($v)) { $out = []; foreach ($v as $k => $x) { $out[$k] = walk($x, $fn); } return $out; } return $v; } $translateFn = function (string $s) use (&$cache): string { return translate($s, $cache); }; foreach (glob($dir.'/*.json') ?: [] as $path) { $name = basename($path); $data = json_decode((string) file_get_contents($path), true, 512, JSON_THROW_ON_ERROR); $out = walk($data, $translateFn); file_put_contents($path, json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)."\n"); echo "Updated {$name}\n"; } file_put_contents($cacheFile, json_encode($cache, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); echo 'Done. Cache: '.$cacheFile."\n";