[PHP-WEBMASTER] [web-php] master: Add function for translation of UI elements (#1057)

Author: haszi (haszi)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2024-10-14T11:41:38+01:00

Commit: Add function for translation of UI elements (#1057) · php/web-php@83705ef · GitHub
Raw diff: https://github.com/php/web-php/commit/83705efdd0e217090aaa7b06d98dfcc8aacb8445.diff

Add function for translation of UI elements (#1057)

Co-authored-by: haszi <haszika80@gmail.com>

Changed paths:
  A include/ui_translation/de.ini
  A include/ui_translation/en.ini
  A include/ui_translation/es.ini
  A include/ui_translation/fr.ini
  A include/ui_translation/it.ini
  A include/ui_translation/ja.ini
  A include/ui_translation/pt_br.ini
  A include/ui_translation/ru.ini
  A include/ui_translation/tr.ini
  A include/ui_translation/zh.ini
  M include/shared-manual.inc

Diff:

diff --git a/include/shared-manual.inc b/include/shared-manual.inc
index d14d6c4565..f104d1549d 100644
--- a/include/shared-manual.inc
+++ b/include/shared-manual.inc
@@ -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&quot; 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&quot; 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 ---&quot;&gt;Report a Bug</a>
+ <a href="https://github.com/php/doc-\{$repo\}/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2F$id ---&quot;&gt;$reportBug&lt;/a&gt;
       </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);
+}
diff --git a/include/ui_translation/de.ini b/include/ui_translation/de.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/de.ini
@@ -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 = ""
diff --git a/include/ui_translation/en.ini b/include/ui_translation/en.ini
new file mode 100644
index 0000000000..32ae383609
--- /dev/null
+++ b/include/ui_translation/en.ini
@@ -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."
diff --git a/include/ui_translation/es.ini b/include/ui_translation/es.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/es.ini
@@ -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 = ""
diff --git a/include/ui_translation/fr.ini b/include/ui_translation/fr.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/fr.ini
@@ -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 = ""
diff --git a/include/ui_translation/it.ini b/include/ui_translation/it.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/it.ini
@@ -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 = ""
diff --git a/include/ui_translation/ja.ini b/include/ui_translation/ja.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/ja.ini
@@ -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 = ""
diff --git a/include/ui_translation/pt_br.ini b/include/ui_translation/pt_br.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/pt_br.ini
@@ -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 = ""
diff --git a/include/ui_translation/ru.ini b/include/ui_translation/ru.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/ru.ini
@@ -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 = ""
diff --git a/include/ui_translation/tr.ini b/include/ui_translation/tr.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/tr.ini
@@ -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 = ""
diff --git a/include/ui_translation/zh.ini b/include/ui_translation/zh.ini
new file mode 100644
index 0000000000..e24fe76b9e
--- /dev/null
+++ b/include/ui_translation/zh.ini
@@ -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 = ""