Skip to content

Commit bee4262

Browse files
author
haszi
committed
Add function for translation of UI elements
1 parent 6e76f1a commit bee4262

File tree

11 files changed

+143
-10
lines changed

11 files changed

+143
-10
lines changed

include/shared-manual.inc

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ use phpweb\UserNotes\UserNote;
3333
* @param array<string, UserNote> $notes
3434
*/
3535
function manual_notes($notes):void {
36+
global $LANG;
37+
3638
// Get needed values
3739
list($filename) = $GLOBALS['PGI']['this'];
3840

@@ -44,13 +46,14 @@ function manual_notes($notes):void {
4446
$sorter = new Sorter();
4547
$sorter->sort($notes);
4648

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

5659
$num_notes = count($notes);
@@ -59,17 +62,19 @@ function manual_notes($notes):void {
5962
$noteCountHtml = "<span class=\"count\">$num_notes note" . ($num_notes == 1 ? '' : 's') . "</span>";
6063
}
6164

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

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

337342
function manual_language_chooser($currentlang, $currentpage) {
338-
global $ACTIVE_ONLINE_LANGUAGES;
343+
global $ACTIVE_ONLINE_LANGUAGES, $LANG;
339344

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

359+
$changeLanguage = autogen('change_language', $LANG);
354360
$r = <<<CHANGE_LANG
355361
<form action="/manual/change.php" method="get" id="changelang" name="changelang">
356362
<fieldset>
357-
<label for="changelang-langs">Change language:</label>
363+
<label for="changelang-langs">$changeLanguage:</label>
358364
<select onchange="document.changelang.submit()" name="page" id="changelang-langs">
359365
{$format_options}
360366
</select>
@@ -365,7 +371,7 @@ CHANGE_LANG;
365371
}
366372

367373
function manual_footer($setup): void {
368-
global $USERNOTES, $__RELATED;
374+
global $USERNOTES, $__RELATED, $LANG;
369375

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

403+
$improveThisPage = autogen('improve_this_page', $LANG);
404+
$howToImproveThisPage = autogen('how_to_improve_this_page', $LANG);
405+
$contributionGuidlinesOnGithub = autogen('contribution_guidlines_on_github', $LANG);
406+
$submitPullRequest = autogen('submit_a_pull_request', $LANG);
407+
$reportBug = autogen('report_a_bug', $LANG);
397408
echo <<<CONTRIBUTE
398409
<div class="contribute">
399-
<h3 class="title">Improve This Page</h3>
410+
<h3 class="title">$improveThisPage</h3>
400411
<div>
401412
$lastUpdate $contributors
402413
</div>
403414
<div class="edit-bug">
404-
<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>
415+
<a href="https://github.com/php/doc-base/blob/master/README.md" title="$contributionGuidlinesOnGithub" target="_blank" rel="noopener noreferrer">$howToImproveThisPage</a>
405416
406-
<a href="{$edit_url}">Submit a Pull Request</a>
417+
<a href="{$edit_url}">$submitPullRequest</a>
407418
408-
<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>
419+
<a href="https://github.com/php/doc-{$repo}/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2F$id%0A%0A---">$reportBug</a>
409420
</div>
410421
</div>
411422
CONTRIBUTE;
@@ -460,3 +471,35 @@ CONTRIBUTORS;
460471
manual_footer($setup);
461472
exit;
462473
}
474+
475+
function autogen(string $text, string $lang) {
476+
static $translations = [];
477+
478+
$lang = ($lang === "") ? "en" : $lang;
479+
if (isset($translations[$lang])) {
480+
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
481+
return $translations[$lang][$text];
482+
}
483+
if ($lang !== "en") {
484+
// fall back to English if text is not defined for the given language
485+
return autogen($text, "en");
486+
}
487+
// we didn't find the English text either
488+
throw new \InvalidArgumentException("Cannot autogenerate text for '$text'");
489+
}
490+
491+
$translationFile = __DIR__ . \DIRECTORY_SEPARATOR . "ui_translation" . \DIRECTORY_SEPARATOR . $lang . ".ini";
492+
493+
if (!\file_exists($translationFile)) {
494+
if ($lang !== "en") {
495+
// fall back to English if translation file is not found
496+
return autogen($text, "en");
497+
}
498+
// we didn't find the English file either
499+
throw new \Exception("Cannot find translation files");
500+
}
501+
502+
$translations[$lang] = \parse_ini_file($translationFile);
503+
504+
return autogen($text, $lang);
505+
}

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)