Author: Kamil Tekiela (kamil-tekiela) Committer: GitHub (web-flow) Pusher: kamil-tekiela Date: 2022-09-29T10:29:51+01:00
Commit: https://github.com/php/web-php/commit/9a5e56d966fa6f39df85b93fa953729e90882a90 Raw diff: https://github.com/php/web-php/commit/9a5e56d966fa6f39df85b93fa953729e90882a90.diff Improve code for language selection (#709) Changed paths: M include/languages.inc M include/prepend.inc M include/shared-manual.inc M my.php Diff: diff --git a/include/languages.inc b/include/languages.inc index 283cbeee9..0ab82e8a9 100644 --- a/include/languages.inc +++ b/include/languages.inc @@ -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'; } } diff --git a/include/prepend.inc b/include/prepend.inc index 7e31a22a3..13f901dff 100644 --- a/include/prepend.inc +++ b/include/prepend.inc @@ -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; diff --git a/include/shared-manual.inc b/include/shared-manual.inc index 90be5acb0..5b973140e 100644 --- a/include/shared-manual.inc +++ b/include/shared-manual.inc @@ -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> diff --git a/my.php b/my.php index 2b5a74d02..a49d8350a 100644 --- a/my.php +++ b/my.php @@ -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>' . -- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php