-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement Accept-Language middleware and German translations #89
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0835829
feat: implement Accept-Language middleware and German translations
kevalyq 94b0232
chore: add REUSE license files for translation assets
kevalyq a375805
fix: add type safety for locale fallback in middleware
kevalyq 9369e9b
fix: address Copilot review comments
kevalyq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| namespace App\Http\Middleware; | ||
|
|
||
| use Closure; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\App; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class SetLocaleFromHeader | ||
| { | ||
| /** | ||
| * Handle an incoming request and set the application locale based on Accept-Language header. | ||
| * | ||
| * The middleware reads the Accept-Language header from the request and sets the application | ||
| * locale accordingly. If no header is present or the language is not supported, it defaults | ||
| * to the application's configured locale. | ||
| * | ||
| * Supported locales: en (English), de (German) | ||
| * | ||
| * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
| */ | ||
| public function handle(Request $request, Closure $next): Response | ||
| { | ||
| $supportedLocales = ['en', 'de']; | ||
|
|
||
| // Get preferred language from Accept-Language header | ||
| $locale = $request->getPreferredLanguage($supportedLocales); | ||
|
|
||
| // Fall back to default if no match | ||
| if ($locale === null) { | ||
| $defaultLocale = config('app.locale'); | ||
| $locale = is_string($defaultLocale) ? $defaultLocale : 'en'; | ||
| } | ||
|
|
||
| App::setLocale($locale); | ||
|
|
||
| return $next($request); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"timestamp":1762116846} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| SPDX-License-Identifier: CC0-1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "Reset Your Password": "Passwort zurücksetzen", | ||
| "Hello": "Hallo", | ||
| "You requested a password reset for your SecPal account.": "Sie haben eine Passwort-Zurücksetzung für Ihr SecPal-Konto angefordert.", | ||
| "Click the button below to reset your password. This link will expire in :minutes minutes.": "Klicken Sie auf die Schaltfläche unten, um Ihr Passwort zurückzusetzen. Dieser Link läuft in :minutes Minuten ab.", | ||
| "Reset Password": "Passwort zurücksetzen", | ||
| "Security Notice:": "Sicherheitshinweis:", | ||
| "Never share this link with anyone": "Teilen Sie diesen Link niemals mit anderen", | ||
| "If you didn't request this reset, please ignore this email": "Wenn Sie diese Zurücksetzung nicht angefordert haben, ignorieren Sie diese E-Mail bitte", | ||
| "Your password will remain unchanged until you complete the reset process": "Ihr Passwort bleibt unverändert, bis Sie den Zurücksetzungsvorgang abschließen", | ||
| "Thanks": "Vielen Dank", | ||
| "If you're having trouble clicking the \":button\" button, copy and paste the URL below into your web browser:": "Falls Sie Probleme beim Klicken auf die Schaltfläche \":button\" haben, kopieren Sie die folgende URL und fügen Sie sie in Ihren Webbrowser ein:" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| SPDX-License-Identifier: CC0-1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| use Illuminate\Support\Facades\App; | ||
|
|
||
| test('middleware sets locale from Accept-Language header to German', function (): void { | ||
| $response = $this->withHeaders([ | ||
| 'Accept-Language' => 'de', | ||
| ])->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe('de'); | ||
| $response->assertOk(); | ||
| }); | ||
|
|
||
| test('middleware sets locale from Accept-Language header to English', function (): void { | ||
| $response = $this->withHeaders([ | ||
| 'Accept-Language' => 'en', | ||
| ])->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe('en'); | ||
| $response->assertOk(); | ||
| }); | ||
|
|
||
| test('middleware defaults to configured locale when no Accept-Language header', function (): void { | ||
| $response = $this->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe(config('app.locale')); | ||
| $response->assertOk(); | ||
| }); | ||
|
|
||
| test('middleware defaults to configured locale for unsupported language', function (): void { | ||
| $response = $this->withHeaders([ | ||
| 'Accept-Language' => 'fr', | ||
| ])->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe(config('app.locale')); | ||
| $response->assertOk(); | ||
| }); | ||
|
|
||
| test('middleware handles complex Accept-Language header with quality values', function (): void { | ||
| $response = $this->withHeaders([ | ||
| 'Accept-Language' => 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7', | ||
| ])->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe('de'); | ||
| $response->assertOk(); | ||
| }); | ||
|
|
||
| test('middleware prefers higher quality language from Accept-Language header', function (): void { | ||
| $response = $this->withHeaders([ | ||
| 'Accept-Language' => 'en;q=0.5,de;q=0.9', | ||
| ])->get('/api/health'); | ||
|
|
||
| expect(App::getLocale())->toBe('de'); | ||
| $response->assertOk(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| use Illuminate\Support\Facades\App; | ||
|
|
||
| test('password reset translation strings exist in English', function (): void { | ||
| App::setLocale('en'); | ||
|
|
||
| expect(__('Reset Your Password'))->toBe('Reset Your Password') | ||
| ->and(__('Hello'))->toBe('Hello') | ||
| ->and(__('Security Notice:'))->toBe('Security Notice:') | ||
| ->and(__('Thanks'))->toBe('Thanks') | ||
| ->and(__('Reset Password'))->toBe('Reset Password'); | ||
| }); | ||
|
|
||
| test('password reset translation strings exist in German', function (): void { | ||
| App::setLocale('de'); | ||
|
|
||
| expect(__('Reset Your Password'))->toBe('Passwort zurücksetzen') | ||
| ->and(__('Hello'))->toBe('Hallo') | ||
| ->and(__('Security Notice:'))->toBe('Sicherheitshinweis:') | ||
| ->and(__('Thanks'))->toBe('Vielen Dank') | ||
| ->and(__('Reset Password'))->toBe('Passwort zurücksetzen'); | ||
| }); | ||
|
|
||
| test('password reset translation with parameters works in English', function (): void { | ||
| App::setLocale('en'); | ||
|
|
||
| $translated = __('Click the button below to reset your password. This link will expire in :minutes minutes.', ['minutes' => 60]); | ||
|
|
||
| expect($translated)->toContain('60 minutes') | ||
| ->and($translated)->toContain('Click the button'); | ||
| }); | ||
|
|
||
| test('password reset translation with parameters works in German', function (): void { | ||
| App::setLocale('de'); | ||
|
|
||
| $translated = __('Click the button below to reset your password. This link will expire in :minutes minutes.', ['minutes' => 60]); | ||
|
|
||
| expect($translated)->toContain('60 Minuten') | ||
| ->and($translated)->toContain('Klicken Sie auf die Schaltfläche'); | ||
| }); | ||
|
|
||
| test('password reset button text translation with parameters works', function (): void { | ||
| App::setLocale('de'); | ||
|
|
||
| $translated = __('If you\'re having trouble clicking the ":button" button, copy and paste the URL below into your web browser:', [ | ||
| 'button' => __('Reset Password'), | ||
| ]); | ||
|
|
||
| expect($translated) | ||
| ->toContain('Passwort zurücksetzen') | ||
| ->toContain('kopieren Sie die folgende URL'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.