Skip to content

Commit 9a5e56d

Browse files
Improve code for language selection (#709)
1 parent 8b776fb commit 9a5e56d

File tree

4 files changed

+29
-61
lines changed

4 files changed

+29
-61
lines changed

include/languages.inc

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,16 @@ $ACTIVE_ONLINE_LANGUAGES = array_diff($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES);
9090
// Convert between language codes back and forth
9191
// [We use non standard languages codes and so conversion
9292
// is needed when communicating with the outside world]
93-
function language_convert($langcode, $to_phpweb_format = true)
93+
function language_convert(string $langcode): string
9494
{
9595
global $LANGUAGES;
96-
if ($to_phpweb_format) {
97-
switch ($langcode) {
98-
case 'zh_cn': return 'zh';
99-
case 'zh_hk': return 'hk';
100-
case 'zh_tw': return 'tw';
101-
case 'ko' : return 'kr';
102-
default:
103-
if (isset($LANGUAGES[$langcode])) {
104-
return $langcode;
105-
}
106-
// Fallback on english if we got something wacky
107-
return "en";
108-
}
109-
}
110-
else {
111-
switch ($langcode) {
112-
case 'cn': return 'zh_cn';
113-
case 'hk': return 'zh_hk';
114-
case 'tw': return 'zh_tw';
115-
case 'kr': return 'ko';
116-
default:
117-
if (isset($LANGUAGES[$langcode])) {
118-
return $langcode;
119-
}
120-
// Fallback on english if we got something wacky
121-
return "en";
122-
}
96+
switch ($langcode) {
97+
case 'zh_cn': return 'zh';
98+
case 'zh_hk': return 'hk';
99+
case 'zh_tw': return 'tw';
100+
case 'ko' : return 'kr';
101+
default:
102+
// Fallback on english if we got something wacky
103+
return array_key_exists($langcode, $LANGUAGES) ? $langcode : 'en';
123104
}
124105
}

include/prepend.inc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,16 @@ function myphpnet_load()
114114
$MYPHPNET[2] = 'https://www.php.net';
115115
}
116116

117-
// Get or set preferred language code
118-
function myphpnet_language($langcode = false)
117+
// Get preferred language code
118+
function myphpnet_language(): string
119119
{
120-
global $MYPHPNET, $LANGUAGES;
120+
global $MYPHPNET;
121121

122-
// Set language code
123-
if ($langcode && isset($LANGUAGES[$langcode])) {
124-
$MYPHPNET[0] = $langcode;
125-
}
126-
// Return code or FALSE
127-
elseif (isset($MYPHPNET[0]) && $MYPHPNET[0]) {
122+
// Return code
123+
if (isset($MYPHPNET[0]) && $MYPHPNET[0]) {
128124
return $MYPHPNET[0];
129125
}
130-
return false;
126+
return '';
131127
}
132128

133129
const MYPHPNET_URL_NONE = false;

include/shared-manual.inc

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -354,35 +354,26 @@ PAGE_TOOLS;
354354
function manual_language_chooser($currentlang, $currentpage) {
355355
global $ACTIVE_ONLINE_LANGUAGES;
356356

357-
$links = [];
358-
359-
foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $name) {
360-
$links[] = ["$lang/$currentpage", $name, $lang];
361-
}
362-
363-
// Print out the form with all the options
357+
// Prepare the form with all the options
364358
$othersel = ' selected="selected"';
365-
$format_options = function (array $links) use ($currentlang, &$othersel) {
366-
$out = '';
367-
$tab = str_repeat(' ', 6);
368-
foreach ($links as $link) {
369-
list($value, $text, $lang) = $link;
370-
$selected = '';
371-
if ($lang == $currentlang) {
372-
$selected = ' selected="selected"';
373-
$othersel = '';
374-
}
375-
$out .= "$tab<option value='$value'$selected>$text</option>\n";
359+
$out = [];
360+
foreach ($ACTIVE_ONLINE_LANGUAGES as $lang => $text) {
361+
$selected = '';
362+
if ($lang == $currentlang) {
363+
$selected = ' selected="selected"';
364+
$othersel = '';
376365
}
377-
return trim($out);
378-
};
366+
$out[] = "<option value='$lang/$currentpage'$selected>$text</option>";
367+
}
368+
$out[] = "<option value='help-translate.php'{$othersel}>Other</option>";
369+
$format_options = implode("\n" . str_repeat(' ', 6), $out);
370+
379371
$r = <<<CHANGE_LANG
380372
<form action="/manual/change.php" method="get" id="changelang" name="changelang">
381373
<fieldset>
382374
<label for="changelang-langs">Change language:</label>
383375
<select onchange="document.changelang.submit()" name="page" id="changelang-langs">
384-
{$format_options($links)}
385-
<option value="help-translate.php"{$othersel}>Other</option>
376+
{$format_options}
386377
</select>
387378
</fieldset>
388379
</form>

my.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if (isset($_POST['my_lang'], $langs[$_POST['my_lang']])) {
1414

1515
// Set the language preference
16-
myphpnet_language($_POST['my_lang']);
16+
$MYPHPNET[0] = $_POST['my_lang'];
1717

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

0 commit comments

Comments
 (0)