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

feat(go.php): Detect browser language to redirect to user manual in preferred language #12485

Merged
merged 2 commits into from
Jan 14, 2025
Merged
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
49 changes: 42 additions & 7 deletions go.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,48 @@

header('HTTP/1.1 302 Moved Temporarily');
if (array_key_exists($from, $mapping)) {
header('Location: ' . $location . $mapping[$from]);
$subPath = $mapping[$from];
} elseif (str_starts_with($from, 'admin-')) {
$subPath = '/admin_manual';
} elseif (str_starts_with($from, 'developer-')) {
$subPath = '/developer_manual';
} else {
if (strpos($from, 'admin-') === 0) {
header('Location: ' . $location . '/admin_manual');
} else if (strpos($from, 'developer-') === 0) {
header('Location: ' . $location . '/developer_manual');
} else {
header('Location: ' . $location . '/user_manual');
$subPath = '/user_manual';
}

$subPathParts = explode('/', $subPath);
$manual = $subPathParts[1] ?? '';
if ($manual === 'user_manual') {
// Sort accepted languages by their weight
$acceptLanguages = array_reduce(
explode(', ', $_SERVER['HTTP_ACCEPT_LANGUAGE']),
static function ($out, $element) {
[$language, $q] = array_merge(explode(';q=', $element), [1]);

$out[$language] = (float)$q;

return $out;
},
[],
);
arsort($acceptLanguages);

foreach ($acceptLanguages as $language => $weight) {
if (!preg_match('/^[a-z-]+$/im', $language)) {
// Skip any invalid languages and prevent directory traversals
continue;
}

$language = str_replace('-', '_', $language);

// To test locally add '/_build/html/' to the path.
if (file_exists(__DIR__ . '/user_manual/' . $language)) {
// Insert the language /user_manual/<language>/...
array_splice($subPathParts, 2, 0, [$language]);
$subPath = implode('/', $subPathParts);
break;
}
}
}

header('Location: ' . $location . $subPath);
Loading