Skip to content

Commit ea4e520

Browse files
committed
autofill language from Accept-Language
1 parent fc59034 commit ea4e520

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

assets/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function initUploadForm() {
127127
return;
128128
}
129129

130+
const localeSelect = document.getElementById('locale');
131+
if (localeSelect.value === '') {
132+
showError('Please choose a roadbook language.');
133+
localeSelect.focus();
134+
return;
135+
}
136+
130137
const payload = {
131138
gpx: gpxContent,
132139
referenceCode,

src/Controller/GeoroadBookController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public function __construct(
4242
}
4343

4444
#[Route('/', name: 'app_homepage', methods: ['GET'])]
45-
public function index(): Response
45+
public function index(Request $request): Response
4646
{
4747
$params = [
4848
'suffix_css_js' => 'aa',
4949
'locales' => $this->locales,
50+
'language' => $this->detectBrowserLocale($request),
5051
];
5152

5253
$user = $this->getUser();
@@ -69,6 +70,18 @@ public function index(): Response
6970
return $this->render('index.html.twig', $params);
7071
}
7172

73+
private function detectBrowserLocale(Request $request): ?string
74+
{
75+
foreach ($request->getLanguages() as $language) {
76+
$code = strtolower(substr($language, 0, 2));
77+
if (isset($this->locales[$code])) {
78+
return $code;
79+
}
80+
}
81+
82+
return null;
83+
}
84+
7285
#[Route('/upload', name: 'app_upload', methods: ['POST'])]
7386
public function upload(Request $request): JsonResponse
7487
{

templates/index.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{# Hero Section #}
66
<div class="hero-section text-center">
77
<div class="container">
8-
<span class="section-label">The ultimate geocaching tool</span>
8+
<span class="section-label">The ultimate geocaching roadbook tool</span>
99
<h1 class="section-title">Ready for your next adventure?</h1>
1010
<p class="page-desc">Create your roadbook. Generate a customizable, ready-to-print trail booklet from your GPX files.</p>
1111
</div>
@@ -60,8 +60,8 @@
6060

6161
<div>
6262
<label class="form-label form-label-mono" for="locale">Roadbook language</label>
63-
<select name="locale" class="form-select" id="locale">
64-
<option value=""></option>
63+
<select name="locale" class="form-select" id="locale" required>
64+
<option value="">Choose a language…</option>
6565
{% for code, name in locales %}
6666
<option value="{{ code|e }}"{% if language is defined and language == code %} selected{% endif %}>{{ name|e }}</option>
6767
{% endfor %}

0 commit comments

Comments
 (0)