Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for translation of UI elements #1057

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe
$noteCountHtml = "<span class=\"count\">$num_notes " . ($num_notes == 1 ? $note : $notes) . "</span>";
to allow translation of the number of notes beside the title?

$note = 'note';
$notes = 'notes';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be much better! Appending an "s" works for few languages. However, pluralization is more difficult than distinguishing between singular and plural (@mvorisek can probably confirm for the Czech language), but it might be okay for the currently active languages (and we could still extend).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm appending s to make words plular is NOT how Czech language works. We change the word ending based on many rules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also for Italian the plurals are not formed by appending s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or use the more universal format «notes: N»,

because, for example, in Russian (I guess it's not the only language),
different endings will be for 1, 3, and 5 notes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because, for example, in Russian (I guess it's not the only language),
different endings will be for 1, 3, and 5 notes.

That's what I was referring to regarding the Czech language. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why gettext has ngettext(), but maybe using that would be overkill for this issue. If there was interest in doing more widespread internationalization of the PHP website, that could be a way to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe $noteCountHtml = "<span class=\"count\">$num_notes " . ($num_notes == 1 ? $note : $notes) . "</span>"; to allow translation of the number of notes beside the title?

I think that doing this is a good idea but I would prefer not to do it as part of this PR. The changes in this PR address the simpler issue of translating the static elements of the documentation UI. Looking at the above discussion on the intricacies of translating the number of notes to various languages (and we haven't even heard from the Chinese or Japanese doc maintainers on the subject), I think all that needs a bit more thought put into it and should be split into its own PR.

So my suggestion is for this PR to only address the translation of the static elements of the doc pages and to open a new PR for the dynamic elements. Maybe even open an issue first and ask maintainers of every language repo to discuss how the dynamic elements (the total note count, the number of upvotes/downvotes and how long ago a note was submitted) can be translated to each of their respective languages and open a PR once it is clear how to adequately implement it for each of those languages.

}

$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 = ""
saundefined marked this conversation as resolved.
Show resolved Hide resolved
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 = ""
Loading