fix: translate.php Gemini payload + drop /<lang>/ from php.net URLs#2403
Open
dunglas wants to merge 4 commits into
Open
fix: translate.php Gemini payload + drop /<lang>/ from php.net URLs#2403dunglas wants to merge 4 commits into
dunglas wants to merge 4 commits into
Conversation
- 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).
Contributor
There was a problem hiding this comment.
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.phpGemini request payload using top-levelsystemInstructionandpartsarrays. - 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 withhttps://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 returnfalse(e.g., on invalid UTF-8 in the prompt/files). In that casestrlen($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 forfalseand 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 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); | |||
| ```markdown | ||
| PROMPT; | ||
| $userPrompt = <<<MD | ||
| Here is the english version of the document: |
| # 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 |
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
docs/translate.php: rebuild the Gemini request payload to match the v1beta API contract — use top-levelsystemInstructionand makepartsan array of objects. The previous shape (role: "model"first +partsas a single object) returnedHTTP 400(see run 25736017574).translate.php/en/,/es/,/fr/,/zh/language segments from everyphp.net/manual/...URL acrossdocs/**/*.mdso links resolve to the visitor's preferred language.