Skip to content

Commit

Permalink
Add function for translation of UI elements (#1057)
Browse files Browse the repository at this point in the history
Co-authored-by: haszi <[email protected]>
  • Loading branch information
haszi and haszi authored Oct 14, 2024
1 parent ea8633e commit 83705ef
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 11 deletions.
65 changes: 54 additions & 11 deletions include/shared-manual.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -42,14 +44,15 @@ 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 .
'&amp;repo=' . $repo .
'&amp;redirect=' . $_SERVER['BASE_HREF'];
$addnotesnippet = make_link(
$addnotelink,
"+<small>add a note</small>",
"+<small>$addNote</small>",
);

$num_notes = count($notes);
Expand All @@ -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">';
Expand Down Expand Up @@ -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"';
Expand All @@ -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>
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
9 changes: 9 additions & 0 deletions include/ui_translation/de.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/en.ini
Original file line number Diff line number Diff line change
@@ -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."
9 changes: 9 additions & 0 deletions include/ui_translation/es.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/fr.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/it.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/ja.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/pt_br.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/ru.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/tr.ini
Original file line number Diff line number Diff line change
@@ -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 = ""
9 changes: 9 additions & 0 deletions include/ui_translation/zh.ini
Original file line number Diff line number Diff line change
@@ -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 = ""

0 comments on commit 83705ef

Please sign in to comment.