Skip to content

fix: translate.php Gemini payload + drop /<lang>/ from php.net URLs#2403

Open
dunglas wants to merge 4 commits into
mainfrom
fix/translate-and-php-net-urls
Open

fix: translate.php Gemini payload + drop /<lang>/ from php.net URLs#2403
dunglas wants to merge 4 commits into
mainfrom
fix/translate-and-php-net-urls

Conversation

@dunglas
Copy link
Copy Markdown
Member

@dunglas dunglas commented May 12, 2026

Summary

  • docs/translate.php: rebuild the Gemini request payload to match the v1beta API contract — use top-level systemInstruction and make parts an array of objects. The previous shape (role: "model" first + parts as a single object) returned HTTP 400 (see run 25736017574).
  • Refresh the system and user prompts in translate.php
  • Drop the /en/, /es/, /fr/, /zh/ language segments from every php.net/manual/... URL across docs/**/*.md so links resolve to the visitor's preferred language.

- translate.php: use top-level systemInstruction and array-of-parts
  payload (Gemini 400'd on role:model + parts-as-object).
- Refresh translate.php system/user prompts.
- Strip /en/, /es/, /fr/, /zh/ language segments from php.net URLs
  in all English and translated docs (use language-agnostic form).
Copilot AI review requested due to automatic review settings May 12, 2026 13:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the docs translation helper to send a Gemini v1beta-compatible request payload, and normalizes php.net manual links across the documentation by removing language-specific URL segments so links resolve in the visitor’s preferred language.

Changes:

  • Rebuild docs/translate.php Gemini request payload using top-level systemInstruction and parts arrays.
  • Refresh/clarify system + user prompts used for translation, including a rule to rewrite php.net URLs to language-agnostic form.
  • Replace https://www.php.net/manual/<lang>/... links with https://www.php.net/manual/... across English + translated docs.

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
docs/translate.php Updates Gemini request payload shape and refines translation prompts/rules.
docs/worker.md Removes language segment from php.net superglobals manual link.
docs/performance.md Removes language segment from php.net OPcache/preloading/FPM config links.
docs/known-issues.md Removes language segment from php.net extension/function links.
docs/config.md Removes language segment from php.net ini/config documentation links.
docs/cn/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/cn/performance.md Normalizes php.net performance-related manual links.
docs/cn/known-issues.md Normalizes php.net known-issues-related manual links.
docs/cn/config.md Normalizes php.net ini/config documentation links.
docs/es/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/es/performance.md Normalizes php.net performance-related manual links.
docs/es/known-issues.md Normalizes php.net known-issues-related manual links.
docs/es/config.md Normalizes php.net ini/config documentation links.
docs/fr/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/fr/performance.md Normalizes php.net performance-related manual links.
docs/fr/known-issues.md Normalizes php.net known-issues-related manual links.
docs/fr/config.md Normalizes php.net ini/config documentation links.
docs/ja/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/ja/performance.md Normalizes php.net performance-related manual links.
docs/ja/known-issues.md Normalizes php.net known-issues-related manual links.
docs/ja/config.md Normalizes php.net ini/config documentation links.
docs/pt-br/config.md Normalizes php.net ini/config documentation links.
docs/ru/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/ru/performance.md Normalizes php.net performance-related manual links.
docs/ru/known-issues.md Normalizes php.net known-issues-related manual links.
docs/ru/config.md Normalizes php.net ini/config documentation links.
docs/tr/worker.md Normalizes php.net superglobals manual link to language-agnostic form.
docs/tr/performance.md Normalizes php.net performance-related manual links.
docs/tr/known-issues.md Normalizes php.net known-issues-related manual links.
docs/tr/config.md Normalizes php.net ini/config documentation links.
Comments suppressed due to low confidence (1)

docs/translate.php:50

  • json_encode() can return false (e.g., on invalid UTF-8 in the prompt/files). In that case strlen($body) will throw a TypeError in PHP 8+, and the request will never be sent. Handle JSON encoding errors explicitly (e.g., JSON_THROW_ON_ERROR + try/catch, or check for false and abort with a clear error) before computing Content-Length/POSTing.
    $body = json_encode([
        "systemInstruction" => ["parts" => [["text" => $systemPrompt]]],
        "contents" => [
            ["role" => "user", "parts" => [["text" => $userPrompt]]],
        ],
    ]);

    $response = @file_get_contents($url, false, stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => "Content-Type: application/json\r\nX-Goog-Api-Key: $apiKey\r\nContent-Length: " . strlen($body) . "\r\n",
            'content' => $body,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/translate.php Outdated
Comment on lines 36 to 64
@@ -53,11 +56,14 @@ function makeGeminiRequest(string $systemPrompt, string $userPrompt, string $mod
if (!$response || !$generatedDocs) {
print_r(error_get_last());
print_r($response);

if ($reties > 0) {
echo "Retrying... ($reties retries left)\n";
sleep(SLEEP_SECONDS_BETWEEN_REQUESTS);

return makeGeminiRequest($systemPrompt, $userPrompt, $model, $apiKey, $reties - 1);
Comment thread docs/translate.php Outdated
```markdown
PROMPT;
$userPrompt = <<<MD
Here is the english version of the document:
Comment thread docs/translate.php Outdated
# update all translations to match the english docs
# usage: php docs/translate.php [specific-file.md]
# needs: php with openssl and gemini api key
# needs: php with openssl and Gemini api key
dunglas added 3 commits May 12, 2026 15:47
- createPrompt: branch the user prompt on whether the target translation
  already exists, instead of feeding the model an empty translation block.
- sanitizeMarkdown: strip leading ```/```markdown and trailing ``` via
  regex so optional whitespace or missing language tag don't bleed
  through to the written file.
- main loop: check file_exists before reading the per-language file to
  avoid a PHP warning on first-time translations.
- Rename $reties → $retries (typo).
- Capitalize "English" / "Gemini API key" in script header and STDIN prompt.
- English: ~28 fixes across 13 files (typos, "superior" → "higher",
  "allows to X" → "makes it easy to X" / "provides", "On Mac" →
  "On macOS", filler-opener removals, capitalization).
- French (16 files): titles to sentence case, French typographic spacing
  before colons/semicolons, accent fixes ("inpectez" → "inspectez",
  "echoue" → "échoue"), "200$" → "200 $", drop English fragment in JSON
  logs note, "Build:" → "Construisez :".
- Spanish (17 files): titles to sentence case, gender/article fixes
  ("La Hot Reload" → "El hot reload"), inverted "¡" added, fix broken
  link in performance.md, stray punctuation in logging.md.
- pt-BR (9 files): typo "suite" → "suíte", singular agreement
  "extraia-os" → "extraia-o", drop non-standard "embedagem" →
  "incorporação", missing periods, list markdown break.
- Strip the remaining /pt_BR/ language segment from php.net URLs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants