1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Add function for translation of UI elements (#1057)

Co-authored-by: haszi <haszika80@gmail.com>
This commit is contained in:
haszi
2024-10-14 12:41:38 +02:00
committed by GitHub
parent ea8633edba
commit 83705efdd0
11 changed files with 144 additions and 11 deletions

View File

@@ -30,7 +30,9 @@ use phpweb\UserNotes\UserNote;
*
* @param array<string, UserNote> $notes
*/
function manual_notes($notes, $repo = 'en'):void {
function manual_notes($notes):void {
global $LANG;
// Get needed values
list($filename) = $GLOBALS['PGI']['this'];
@@ -42,6 +44,7 @@ function manual_notes($notes, $repo = 'en'):void {
$sorter = new Sorter();
$sorter->sort($notes);
$addNote = autogen('add_a_note', $LANG);
// Link target to add a note to the current manual page,
// and it's extended form with a [+] image
$addnotelink = '/manual/add-note.php?sect=' . $filename .
@@ -49,7 +52,7 @@ function manual_notes($notes, $repo = 'en'):void {
'&amp;redirect=' . $_SERVER['BASE_HREF'];
$addnotesnippet = make_link(
$addnotelink,
"<small>add a note</small>",
"<small>$addNote</small>",
);
$num_notes = count($notes);
@@ -58,17 +61,19 @@ function manual_notes($notes, $repo = 'en'):void {
$noteCountHtml = "<span class=\"count\">$num_notes note" . ($num_notes == 1 ? '' : 's') . "</span>";
}
$userContributedNotes = autogen('user_contributed_notes', $LANG);
echo <<<END_USERNOTE_HEADER
<section id="usernotes">
<div class="head">
<span class="action">{$addnotesnippet}</span>
<h3 class="title">User Contributed Notes {$noteCountHtml}</h3>
<h3 class="title">$userContributedNotes {$noteCountHtml}</h3>
</div>
END_USERNOTE_HEADER;
// If we have no notes, then inform the user
if ($num_notes === 0) {
echo "\n <div class=\"note\">There are no user contributed notes for this page.</div>";
$noUserContributedNotes = autogen('no_user_notes', $LANG);
echo "\n <div class=\"note\">$noUserContributedNotes</div>";
} else {
// If we have notes, print them out
echo '<div id="allnotes">';
@@ -334,7 +339,7 @@ PAGE_TOOLS;
}
function manual_language_chooser($currentlang, $currentpage) {
global $ACTIVE_ONLINE_LANGUAGES;
global $ACTIVE_ONLINE_LANGUAGES, $LANG;
// Prepare the form with all the options
$othersel = ' selected="selected"';
@@ -350,10 +355,11 @@ function manual_language_chooser($currentlang, $currentpage) {
$out[] = "<option value='help-translate.php'{$othersel}>Other</option>";
$format_options = implode("\n" . str_repeat(' ', 6), $out);
$changeLanguage = autogen('change_language', $LANG);
$r = <<<CHANGE_LANG
<form action="/manual/change.php" method="get" id="changelang" name="changelang">
<fieldset>
<label for="changelang-langs">Change language:</label>
<label for="changelang-langs">$changeLanguage:</label>
<select onchange="document.changelang.submit()" name="page" id="changelang-langs">
{$format_options}
</select>
@@ -364,7 +370,7 @@ CHANGE_LANG;
}
function manual_footer($setup): void {
global $USERNOTES, $__RELATED;
global $USERNOTES, $__RELATED, $LANG;
$id = substr($setup['this'][0], 0, -4);
$repo = strtolower($setup["head"][1]); // pt_BR etc.
@@ -393,18 +399,23 @@ function manual_footer($setup): void {
$contributors = '<a href="?contributors">All contributors.</a>';
}
$improveThisPage = autogen('improve_this_page', $LANG);
$howToImproveThisPage = autogen('how_to_improve_this_page', $LANG);
$contributionGuidlinesOnGithub = autogen('contribution_guidlines_on_github', $LANG);
$submitPullRequest = autogen('submit_a_pull_request', $LANG);
$reportBug = autogen('report_a_bug', $LANG);
echo <<<CONTRIBUTE
<div class="contribute">
<h3 class="title">Improve This Page</h3>
<h3 class="title">$improveThisPage</h3>
<div>
$lastUpdate $contributors
</div>
<div class="edit-bug">
<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>
<a href="https://github.com/php/doc-base/blob/master/README.md" title="$contributionGuidlinesOnGithub" target="_blank" rel="noopener noreferrer">$howToImproveThisPage</a>
<a href="{$edit_url}">Submit a Pull Request</a>
<a href="{$edit_url}">$submitPullRequest</a>
<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>
<a href="https://github.com/php/doc-{$repo}/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2F$id%0A%0A---">$reportBug</a>
</div>
</div>
CONTRIBUTE;
@@ -459,3 +470,35 @@ CONTRIBUTORS;
manual_footer($setup);
exit;
}
function autogen(string $text, string $lang) {
static $translations = [];
$lang = ($lang === "") ? "en" : $lang;
if (isset($translations[$lang])) {
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
return $translations[$lang][$text];
}
if ($lang !== "en") {
// fall back to English if text is not defined for the given language
return autogen($text, "en");
}
// we didn't find the English text either
throw new \InvalidArgumentException("Cannot autogenerate text for '$text'");
}
$translationFile = __DIR__ . \DIRECTORY_SEPARATOR . "ui_translation" . \DIRECTORY_SEPARATOR . $lang . ".ini";
if (!\file_exists($translationFile)) {
if ($lang !== "en") {
// fall back to English if translation file is not found
return autogen($text, "en");
}
// we didn't find the English file either
throw new \Exception("Cannot find translation files");
}
$translations[$lang] = \parse_ini_file($translationFile);
return autogen($text, $lang);
}

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = "Change language"
improve_this_page = "Improve This Page"
how_to_improve_this_page = "Learn How To Improve This Page"
contribution_guidlines_on_github = "This will take you to our contribution guidelines on GitHub"
submit_a_pull_request = "Submit a Pull Request"
report_a_bug = "Report a Bug"
add_a_note = "add a note"
user_contributed_notes = "User Contributed Notes"
no_user_notes = "There are no user contributed notes for this page."

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""

View File

@@ -0,0 +1,9 @@
change_language = ""
improve_this_page = ""
how_to_improve_this_page = ""
contribution_guidlines_on_github = ""
submit_a_pull_request = ""
report_a_bug = ""
add_a_note = ""
user_contributed_notes = ""
no_user_notes = ""