Skip to content

Commit ed8da8f

Browse files
author
haszi
committed
OAdd function for translation of UI elements
1 parent 9a1fd55 commit ed8da8f

File tree

11 files changed

+144
-11
lines changed

11 files changed

+144
-11
lines changed

include/shared-manual.inc

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ use phpweb\UserNotes\UserNote;
3030
*
3131
* @param array<string, UserNote> $notes
3232
*/
33-
function manual_notes($notes, $repo = 'en'):void {
33+
function manual_notes($notes):void {
34+
global $LANG;
35+
3436
// Get needed values
3537
list($filename) = $GLOBALS['PGI']['this'];
3638

@@ -42,14 +44,15 @@ function manual_notes($notes, $repo = 'en'):void {
4244
$sorter = new Sorter();
4345
$sorter->sort($notes);
4446

47+
$addNote = autogen('add_a_note', $LANG);
4548
// Link target to add a note to the current manual page,
4649
// and it's extended form with a [+] image
4750
$addnotelink = '/manual/add-note.php?sect=' . $filename .
4851
'&amp;repo=' . $repo .
4952
'&amp;redirect=' . $_SERVER['BASE_HREF'];
5053
$addnotesnippet = make_link(
5154
$addnotelink,
52-
"+<small>add a note</small>",
55+
"+<small>$addNote</small>",
5356
);
5457

5558
$num_notes = count($notes);
@@ -58,17 +61,19 @@ function manual_notes($notes, $repo = 'en'):void {
5861
$noteCountHtml = "<span class=\"count\">$num_notes note" . ($num_notes == 1 ? '' : 's') . "</span>";
5962
}
6063

64+
$userContributedNotes = autogen('user_contributed_notes', $LANG);
6165
echo <<<END_USERNOTE_HEADER
6266
<section id="usernotes">
6367
<div class="head">
6468
<span class="action">{$addnotesnippet}</span>
65-
<h3 class="title">User Contributed Notes {$noteCountHtml}</h3>
69+
<h3 class="title">$userContributedNotes {$noteCountHtml}</h3>
6670
</div>
6771
END_USERNOTE_HEADER;
6872

6973
// If we have no notes, then inform the user
7074
if ($num_notes === 0) {
71-
echo "\n <div class=\"note\">There are no user contributed notes for this page.</div>";
75+
$noUserContributedNotes = autogen('no_user_notes', $LANG);
76+
echo "\n <div class=\"note\">$noUserContributedNotes</div>";
7277
} else {
7378
// If we have notes, print them out
7479
echo '<div id="allnotes">';
@@ -334,7 +339,7 @@ PAGE_TOOLS;
334339
}
335340

336341
function manual_language_chooser($currentlang, $currentpage) {
337-
global $ACTIVE_ONLINE_LANGUAGES;
342+
global $ACTIVE_ONLINE_LANGUAGES, $LANG;
338343

339344
// Prepare the form with all the options
340345
$othersel = ' selected="selected"';
@@ -350,10 +355,11 @@ function manual_language_chooser($currentlang, $currentpage) {
350355
$out[] = "<option value='help-translate.php'{$othersel}>Other</option>";
351356
$format_options = implode("\n" . str_repeat(' ', 6), $out);
352357

358+
$changeLanguage = autogen('change_language', $LANG);
353359
$r = <<<CHANGE_LANG
354360
<form action="/manual/change.php" method="get" id="changelang" name="changelang">
355361
<fieldset>
356-
<label for="changelang-langs">Change language:</label>
362+
<label for="changelang-langs">$changeLanguage:</label>
357363
<select onchange="document.changelang.submit()" name="page" id="changelang-langs">
358364
{$format_options}
359365
</select>
@@ -364,7 +370,7 @@ CHANGE_LANG;
364370
}
365371

366372
function manual_footer($setup): void {
367-
global $USERNOTES, $__RELATED;
373+
global $USERNOTES, $__RELATED, $LANG;
368374

369375
$id = substr($setup['this'][0], 0, -4);
370376
$repo = strtolower($setup["head"][1]); // pt_BR etc.
@@ -393,18 +399,23 @@ function manual_footer($setup): void {
393399
$contributors = '<a href="?contributors">All contributors.</a>';
394400
}
395401

402+
$improveThisPage = autogen('improve_this_page', $LANG);
403+
$howToImproveThisPage = autogen('how_to_improve_this_page', $LANG);
404+
$contributionGuidlinesOnGithub = autogen('contribution_guidlines_on_github', $LANG);
405+
$submitPullRequest = autogen('submit_a_pull_request', $LANG);
406+
$reportBug = autogen('report_a_bug', $LANG);
396407
echo <<<CONTRIBUTE
397408
<div class="contribute">
398-
<h3 class="title">Improve This Page</h3>
409+
<h3 class="title">$improveThisPage</h3>
399410
<div>
400411
$lastUpdate $contributors
401412
</div>
402413
<div class="edit-bug">
403-
<a href="https://github.com/php/doc-base/blob/master/README.md" title="This will take you to our contribution guidelines on GitHub." target="_blank" rel="noopener noreferrer">Learn How To Improve This Page</a>
414+
<a href="https://github.com/php/doc-base/blob/master/README.md" title="$contributionGuidlinesOnGithub" target="_blank" rel="noopener noreferrer">$howToImproveThisPage</a>
404415
405-
<a href="{$edit_url}">Submit a Pull Request</a>
416+
<a href="{$edit_url}">$submitPullRequest</a>
406417
407-
<a href="https://github.com/php/doc-{$repo}/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2F$id%0A%0A---">Report a Bug</a>
418+
<a href="https://github.com/php/doc-{$repo}/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2F$id%0A%0A---">$reportBug</a>
408419
</div>
409420
</div>
410421
CONTRIBUTE;
@@ -459,3 +470,35 @@ CONTRIBUTORS;
459470
manual_footer($setup);
460471
exit;
461472
}
473+
474+
function autogen(string $text, string $lang) {
475+
static $translations = [];
476+
477+
$lang = ($lang === "") ? "en" : $lang;
478+
if (isset($translations[$lang])) {
479+
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
480+
return $translations[$lang][$text];
481+
}
482+
if ($lang !== "en") {
483+
// fall back to English if text is not defined for the given language
484+
return autogen($text, "en");
485+
}
486+
// we didn't find the English text either
487+
throw new \InvalidArgumentException("Cannot autogenerate text for '$text'");
488+
}
489+
490+
$translationFile = __DIR__ . \DIRECTORY_SEPARATOR . "ui_translation" . \DIRECTORY_SEPARATOR . $lang . ".ini";
491+
492+
if (!\file_exists($translationFile)) {
493+
if ($lang !== "en") {
494+
// fall back to English if translation file is not found
495+
return autogen($text, "en");
496+
}
497+
// we didn't find the English file either
498+
throw new \Exception("Cannot find translation files");
499+
}
500+
501+
$translations[$lang] = \parse_ini_file($translationFile);
502+
503+
return autogen($text, $lang);
504+
}

include/ui_translation/de.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/en.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = "Change language"
2+
improve_this_page = "Improve This Page"
3+
how_to_improve_this_page = "Learn How To Improve This Page"
4+
contribution_guidlines_on_github = "This will take you to our contribution guidelines on GitHub"
5+
submit_a_pull_request = "Submit a Pull Request"
6+
report_a_bug = "Report a Bug"
7+
add_a_note = "add a note"
8+
user_contributed_notes = "User Contributed Notes"
9+
no_user_notes = "There are no user contributed notes for this page."

include/ui_translation/es.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/fr.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/it.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/ja.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/pt_br.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/ru.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/tr.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

include/ui_translation/zh.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
change_language = ""
2+
improve_this_page = ""
3+
how_to_improve_this_page = ""
4+
contribution_guidlines_on_github = ""
5+
submit_a_pull_request = ""
6+
report_a_bug = ""
7+
add_a_note = ""
8+
user_contributed_notes = ""
9+
no_user_notes = ""

0 commit comments

Comments
 (0)