Skip to content

Commit

Permalink
Improve code for language selection (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela committed Sep 29, 2022
1 parent 8b776fb commit 9a5e56d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 61 deletions.
37 changes: 9 additions & 28 deletions include/languages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,16 @@ $ACTIVE_ONLINE_LANGUAGES = array_diff($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES);
// Convert between language codes back and forth
// [We use non standard languages codes and so conversion
// is needed when communicating with the outside world]
function language_convert($langcode, $to_phpweb_format = true)
function language_convert(string $langcode): string
{
global $LANGUAGES;
if ($to_phpweb_format) {
switch ($langcode) {
case 'zh_cn': return 'zh';
case 'zh_hk': return 'hk';
case 'zh_tw': return 'tw';
case 'ko' : return 'kr';
default:
if (isset($LANGUAGES[$langcode])) {
return $langcode;
}
// Fallback on english if we got something wacky
return "en";
}
}
else {
switch ($langcode) {
case 'cn': return 'zh_cn';
case 'hk': return 'zh_hk';
case 'tw': return 'zh_tw';
case 'kr': return 'ko';
default:
if (isset($LANGUAGES[$langcode])) {
return $langcode;
}
// Fallback on english if we got something wacky
return "en";
}
switch ($langcode) {
case 'zh_cn': return 'zh';
case 'zh_hk': return 'hk';
case 'zh_tw': return 'tw';
case 'ko' : return 'kr';
default:
// Fallback on english if we got something wacky
return array_key_exists($langcode, $LANGUAGES) ? $langcode : 'en';
}
}
16 changes: 6 additions & 10 deletions include/prepend.inc
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,16 @@ function myphpnet_load()
$MYPHPNET[2] = 'https://www.php.net';
}

// Get or set preferred language code
function myphpnet_language($langcode = false)
// Get preferred language code
function myphpnet_language(): string
{
global $MYPHPNET, $LANGUAGES;
global $MYPHPNET;

// Set language code
if ($langcode && isset($LANGUAGES[$langcode])) {
$MYPHPNET[0] = $langcode;
}
// Return code or FALSE
elseif (isset($MYPHPNET[0]) && $MYPHPNET[0]) {
// Return code
if (isset($MYPHPNET[0]) && $MYPHPNET[0]) {
return $MYPHPNET[0];
}
return false;
return '';
}

const MYPHPNET_URL_NONE = false;
Expand Down
35 changes: 13 additions & 22 deletions include/shared-manual.inc
Original file line number Diff line number Diff line change
Expand Up @@ -354,35 +354,26 @@ PAGE_TOOLS;
function manual_language_chooser($currentlang, $currentpage) {
global $ACTIVE_ONLINE_LANGUAGES;

$links = [];

foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $name) {
$links[] = ["$lang/$currentpage", $name, $lang];
}

// Print out the form with all the options
// Prepare the form with all the options
$othersel = ' selected="selected"';
$format_options = function (array $links) use ($currentlang, &$othersel) {
$out = '';
$tab = str_repeat(' ', 6);
foreach ($links as $link) {
list($value, $text, $lang) = $link;
$selected = '';
if ($lang == $currentlang) {
$selected = ' selected="selected"';
$othersel = '';
}
$out .= "$tab<option value='$value'$selected>$text</option>\n";
$out = [];
foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
$selected = '';
if ($lang == $currentlang) {
$selected = ' selected="selected"';
$othersel = '';
}
return trim($out);
};
$out[] = "<option value='$lang/$currentpage'$selected>$text</option>";
}
$out[] = "<option value='help-translate.php'{$othersel}>Other</option>";
$format_options = implode("\n" . str_repeat(' ', 6), $out);

$r = <<<CHANGE_LANG
<form action="/manual/change.php" method="get" id="changelang" name="changelang">
<fieldset>
<label for="changelang-langs">Change language:</label>
<select onchange="document.changelang.submit()" name="page" id="changelang-langs">
{$format_options($links)}
<option value="help-translate.php"{$othersel}>Other</option>
{$format_options}
</select>
</fieldset>
</form>
Expand Down
2 changes: 1 addition & 1 deletion my.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if (isset($_POST['my_lang'], $langs[$_POST['my_lang']])) {

// Set the language preference
myphpnet_language($_POST['my_lang']);
$MYPHPNET[0] = $_POST['my_lang'];

// Add this as first option, selected
$options[] = '<option value="' . $_POST['my_lang'] . '" selected>' .
Expand Down

0 comments on commit 9a5e56d

Please sign in to comment.