Skip to content

Commit ff2b3cd

Browse files
committed
init project
0 parents  commit ff2b3cd

File tree

13 files changed

+446
-0
lines changed

13 files changed

+446
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore
11+
/.editorconfig export-ignore

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [samuraee]

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Unit Test
2+
3+
on:
4+
- push
5+
6+
jobs:
7+
run:
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
php-version: ['8.3', '8.4']
13+
laravel-version: [^11, ^12]
14+
15+
steps:
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: ${{ matrix.php-version }}
20+
21+
- uses: actions/checkout@v2
22+
23+
- name: Install laravel
24+
run: composer require illuminate/contracts:${{ matrix.laravel-version }} --no-interaction --prefer-dist --no-progress
25+
26+
- name: Run unit tests
27+
run: vendor/bin/phpunit

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea
2+
build
3+
composer.lock
4+
docs
5+
vendor
6+
coverage
7+
.DS_Store
8+
.php_cs.cache
9+
.phpunit.result.cache

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-accept-language-middleware` will be documented in this file
4+
5+
## 12.0.0 - 2025-03-03
6+
7+
- Initial release

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Aboozar Ghaffari
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Laravel "Accept-Language" middleware
2+
3+
Laravel middleware for automatically setting application locale based on [HTTP "Accept-Language"](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) header
4+
5+
## Requirements
6+
7+
- PHP **8.3** or higher.
8+
- Laravel **^12.0** or higher
9+
10+
## Installation
11+
12+
You can install the package via composer:
13+
14+
```bash
15+
composer require php-monsters/laravel-accept-language-middleware
16+
```
17+
18+
## Usage
19+
20+
Register `\PhpMonsters\LaravelAcceptLanguageMiddleware\Middleware::class` middleware in application's HTTP Kernel.
21+
22+
You can install it as global middleware in Kernel's `$middleware` property:
23+
24+
``` php
25+
protected $middleware = [
26+
...
27+
\PhpMonsters\LaravelAcceptLanguageMiddleware\Middleware::class
28+
];
29+
```
30+
31+
You can install it to specific middleware groups in Kernel's `$middlewareGroups` property:
32+
33+
``` php
34+
protected $middlewareGroups = [
35+
'web' => [
36+
...
37+
\PhpMonsters\LaravelAcceptLanguageMiddleware\Middleware::class
38+
]
39+
];
40+
```
41+
42+
Or you can install is as route middleware in Kernel's `$routeMiddleware` and use it manually in routes:
43+
44+
Kernel:
45+
46+
``` php
47+
protected $routeMiddleware = [
48+
...
49+
'accept-language' => \PhpMonsters\LaravelAcceptLanguageMiddleware\Middleware::class
50+
];
51+
```
52+
53+
Route file
54+
``` php
55+
Route::middleware(['accept-language'])->get('/', 'MyController@index');
56+
```
57+
58+
### Testing
59+
60+
``` bash
61+
composer test
62+
```
63+
64+
### Changelog
65+
66+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
67+
68+
## Credits
69+
70+
- [Aboozar Ghaffari](https://github.com/samuraee)
71+
- [All Contributors](../../contributors)
72+
73+
## License
74+
75+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "php-monsters/laravel-accept-language-middleware",
3+
"description": "Laravel middleware for setting application locale based on HTTP Accept-Language header",
4+
"keywords": [
5+
"language-header",
6+
"accept-language",
7+
"http-language",
8+
"laravel",
9+
"laravel middleware"
10+
],
11+
"homepage": "https://github.com/php-monsters/laravel-accept-language-middleware",
12+
"license": "MIT",
13+
"type": "library",
14+
"authors": [
15+
{
16+
"name": "Aboozar Ghaffari",
17+
"email": "abooazr.ghf@gmail.com",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"php": "^8.3",
23+
"illuminate/contracts": "^12.0",
24+
"illuminate/http": "^13.0",
25+
"illuminate/support": "^12.0"
26+
},
27+
"require-dev": {
28+
"orchestra/testbench": "^10.0",
29+
"phpunit/phpunit": "^11.0"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"PhpMonsters\\LaravelAcceptLanguageMiddleware\\": "src"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"PhpMonsters\\LaravelAcceptLanguageMiddleware\\Tests\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"test": "vendor/bin/phpunit",
43+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
44+
},
45+
"config": {
46+
"sort-packages": true
47+
}
48+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/Middleware.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace PhpMonsters\LaravelAcceptLanguageMiddleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Collection;
8+
9+
class Middleware
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param Request $request
15+
* @param \Closure $next
16+
* @return mixed
17+
*/
18+
public function handle($request, Closure $next)
19+
{
20+
if ($locale = $this->parseHttpLocale($request)) {
21+
app()->setLocale($locale);
22+
}
23+
24+
return $next($request);
25+
}
26+
27+
private function parseHttpLocale(Request $request): string
28+
{
29+
$list = explode(',', $request->server('HTTP_ACCEPT_LANGUAGE', ''));
30+
31+
$locales = Collection::make($list)
32+
->map(function ($locale) {
33+
$parts = explode(';', $locale);
34+
35+
$mapping['locale'] = trim($parts[0]);
36+
37+
if (isset($parts[1])) {
38+
$factorParts = explode('=', $parts[1]);
39+
40+
$mapping['factor'] = $factorParts[1];
41+
} else {
42+
$mapping['factor'] = 1;
43+
}
44+
45+
return $mapping;
46+
})
47+
->sortByDesc(function ($locale) {
48+
return $locale['factor'];
49+
});
50+
51+
return $locales->first()['locale'];
52+
}
53+
}

0 commit comments

Comments
 (0)