Skip to content

Commit d95c2fe

Browse files
authored
Merge codezero/laravel-localizer (#83)
Closes #22
1 parent 170c820 commit d95c2fe

22 files changed

+904
-116
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"require": {
2323
"php": "^7.2.5|^8.0",
2424
"0.0.0/composer-include-files": "^1.5",
25-
"codezero/laravel-localizer": "dev-master",
25+
"codezero/browser-locale": "^3.0",
2626
"codezero/laravel-uri-translator": "^1.0",
2727
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
2828
},

config/localized-routes.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,67 @@
4141
'404_view' => 'errors.404',
4242

4343
/**
44-
* The custom route action where we will set the locale of
45-
* the routes registered within the Route::localized() closure.
44+
* The custom route action where we will set the locale of the routes
45+
* that are registered within the Route::localized() closure.
46+
* This can be detected by the RouteActionDetector.
4647
*/
4748
'route_action' => 'locale',
4849

50+
/**
51+
* The attribute on the user model that holds the locale,
52+
* when using the UserDetector.
53+
*/
54+
'user_attribute' => 'locale',
55+
56+
/**
57+
* The session key that holds the locale,
58+
* when using the SessionDetector and SessionStore.
59+
*/
60+
'session_key' => 'locale',
61+
62+
/**
63+
* The name of the cookie that holds the locale,
64+
* when using the CookieDetector and CookieStore.
65+
*/
66+
'cookie_name' => 'locale',
67+
68+
/**
69+
* The lifetime of the cookie that holds the locale,
70+
* when using the CookieStore.
71+
*/
72+
'cookie_minutes' => 60 * 24 * 365, // 1 year
73+
74+
/**
75+
* The detectors to use to find a matching locale.
76+
* These will be executed in the order that they are added to the array!
77+
*/
78+
'detectors' => [
79+
CodeZero\LocalizedRoutes\Middleware\Detectors\RouteActionDetector::class,
80+
CodeZero\LocalizedRoutes\Middleware\Detectors\UrlDetector::class,
81+
CodeZero\LocalizedRoutes\Middleware\Detectors\OmittedLocaleDetector::class,
82+
CodeZero\LocalizedRoutes\Middleware\Detectors\UserDetector::class,
83+
CodeZero\LocalizedRoutes\Middleware\Detectors\SessionDetector::class,
84+
CodeZero\LocalizedRoutes\Middleware\Detectors\CookieDetector::class,
85+
CodeZero\LocalizedRoutes\Middleware\Detectors\BrowserDetector::class,
86+
CodeZero\LocalizedRoutes\Middleware\Detectors\AppDetector::class,
87+
],
88+
89+
/**
90+
* Add any of the above detector class names here to make it trusted.
91+
* When a trusted detector returns a locale, it will be used
92+
* as the app locale, regardless if it's a supported locale or not.
93+
*/
94+
'trusted_detectors' => [
95+
CodeZero\LocalizedRoutes\Middleware\Detectors\RouteActionDetector::class //=> required for scoped config
96+
],
97+
98+
/**
99+
* The stores to store the first matching locale in.
100+
*/
101+
'stores' => [
102+
CodeZero\LocalizedRoutes\Middleware\Stores\SessionStore::class,
103+
CodeZero\LocalizedRoutes\Middleware\Stores\CookieStore::class,
104+
CodeZero\LocalizedRoutes\Middleware\Stores\AppStore::class,
105+
],
106+
49107
];

src/LocalizedRoutesServiceProvider.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace CodeZero\LocalizedRoutes;
44

5+
use CodeZero\BrowserLocale\Laravel\BrowserLocaleServiceProvider;
56
use CodeZero\LocalizedRoutes\Illuminate\Routing\UrlGenerator;
67
use CodeZero\LocalizedRoutes\Macros\Route\HasLocalizedMacro;
78
use CodeZero\LocalizedRoutes\Macros\Route\isFallbackMacro;
89
use CodeZero\LocalizedRoutes\Macros\Route\IsLocalizedMacro;
910
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedMacro;
1011
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedUrlMacro;
11-
use CodeZero\Localizer\LocalizerServiceProvider;
12+
use CodeZero\LocalizedRoutes\Middleware\LocaleHandler;
1213
use CodeZero\UriTranslator\UriTranslatorServiceProvider;
1314
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
1415
use Illuminate\Support\ServiceProvider;
@@ -42,6 +43,7 @@ public function register()
4243
{
4344
$this->mergeConfig();
4445
$this->registerLocaleConfig();
46+
$this->registerLocaleHandler();
4547
$this->registerUrlGenerator();
4648
$this->registerProviders();
4749
}
@@ -90,7 +92,7 @@ protected function mergeConfig()
9092
*/
9193
protected function registerProviders()
9294
{
93-
$this->app->register(LocalizerServiceProvider::class);
95+
$this->app->register(BrowserLocaleServiceProvider::class);
9496
$this->app->register(UriTranslatorServiceProvider::class);
9597
}
9698

@@ -108,6 +110,23 @@ protected function registerLocaleConfig()
108110
$this->app->bind('locale-config', LocaleConfig::class);
109111
}
110112

113+
/**
114+
* Register LocaleHandler.
115+
*
116+
* @return void
117+
*/
118+
protected function registerLocaleHandler()
119+
{
120+
$this->app->bind(LocaleHandler::class, function ($app) {
121+
$locales = $app['locale-config']->getLocales();
122+
$detectors = $app['config']->get("{$this->name}.detectors");
123+
$stores = $app['config']->get("{$this->name}.stores");
124+
$trustedDetectors = $app['config']->get("{$this->name}.trusted_detectors");
125+
126+
return new LocaleHandler($locales, $detectors, $stores, $trustedDetectors);
127+
});
128+
}
129+
111130
/**
112131
* Register a custom URL generator that extends the one that comes with Laravel.
113132
* This will override a few methods that enables us to generate localized URLs.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use Illuminate\Support\Facades\App;
6+
7+
class AppDetector implements Detector
8+
{
9+
/**
10+
* Detect the locale.
11+
*
12+
* @return string|array|null
13+
*/
14+
public function detect()
15+
{
16+
return App::getLocale();
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use CodeZero\BrowserLocale\BrowserLocale;
6+
use CodeZero\BrowserLocale\Filters\CombinedFilter;
7+
use Illuminate\Support\Facades\App;
8+
9+
class BrowserDetector implements Detector
10+
{
11+
/**
12+
* Detect the locale.
13+
*
14+
* @return string|array|null
15+
*/
16+
public function detect()
17+
{
18+
return App::make(BrowserLocale::class)->filter(new CombinedFilter);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Cookie;
7+
8+
class CookieDetector implements Detector
9+
{
10+
/**
11+
* Detect the locale.
12+
*
13+
* @return string|array|null
14+
*/
15+
public function detect()
16+
{
17+
$key = Config::get('localized-routes.cookie_name');
18+
19+
return Cookie::get($key);
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
interface Detector
6+
{
7+
/**
8+
* Detect the locale.
9+
*
10+
* @return string|array|null
11+
*/
12+
public function detect();
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class OmittedLocaleDetector implements Detector
8+
{
9+
/**
10+
* Detect the locale.
11+
*
12+
* @return string|array|null
13+
*/
14+
public function detect()
15+
{
16+
return Config::get('localized-routes.omitted_locale') ?: null;
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Request;
7+
8+
class RouteActionDetector implements Detector
9+
{
10+
/**
11+
* Detect the locale.
12+
*
13+
* @return string|array|null
14+
*/
15+
public function detect()
16+
{
17+
$action = Config::get('localized-routes.route_action');
18+
19+
return Request::route()->getAction($action);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Session;
7+
8+
class SessionDetector implements Detector
9+
{
10+
/**
11+
* Detect the locale.
12+
*
13+
* @return string|array|null
14+
*/
15+
public function detect()
16+
{
17+
$key = Config::get('localized-routes.session_key');
18+
19+
return Session::get($key);
20+
}
21+
}

0 commit comments

Comments
 (0)