Skip to content

Merge codezero/laravel-localizer #83

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 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^7.2.5|^8.0",
"0.0.0/composer-include-files": "^1.5",
"codezero/laravel-localizer": "dev-master",
"codezero/browser-locale": "^3.0",
"codezero/laravel-uri-translator": "^1.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
},
Expand Down
62 changes: 60 additions & 2 deletions config/localized-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,67 @@
'404_view' => 'errors.404',

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

/**
* The attribute on the user model that holds the locale,
* when using the UserDetector.
*/
'user_attribute' => 'locale',

/**
* The session key that holds the locale,
* when using the SessionDetector and SessionStore.
*/
'session_key' => 'locale',

/**
* The name of the cookie that holds the locale,
* when using the CookieDetector and CookieStore.
*/
'cookie_name' => 'locale',

/**
* The lifetime of the cookie that holds the locale,
* when using the CookieStore.
*/
'cookie_minutes' => 60 * 24 * 365, // 1 year

/**
* The detectors to use to find a matching locale.
* These will be executed in the order that they are added to the array!
*/
'detectors' => [
CodeZero\LocalizedRoutes\Middleware\Detectors\RouteActionDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\UrlDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\OmittedLocaleDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\UserDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\SessionDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\CookieDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\BrowserDetector::class,
CodeZero\LocalizedRoutes\Middleware\Detectors\AppDetector::class,
],

/**
* Add any of the above detector class names here to make it trusted.
* When a trusted detector returns a locale, it will be used
* as the app locale, regardless if it's a supported locale or not.
*/
'trusted_detectors' => [
CodeZero\LocalizedRoutes\Middleware\Detectors\RouteActionDetector::class //=> required for scoped config
],

/**
* The stores to store the first matching locale in.
*/
'stores' => [
CodeZero\LocalizedRoutes\Middleware\Stores\SessionStore::class,
CodeZero\LocalizedRoutes\Middleware\Stores\CookieStore::class,
CodeZero\LocalizedRoutes\Middleware\Stores\AppStore::class,
],

];
23 changes: 21 additions & 2 deletions src/LocalizedRoutesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace CodeZero\LocalizedRoutes;

use CodeZero\BrowserLocale\Laravel\BrowserLocaleServiceProvider;
use CodeZero\LocalizedRoutes\Illuminate\Routing\UrlGenerator;
use CodeZero\LocalizedRoutes\Macros\Route\HasLocalizedMacro;
use CodeZero\LocalizedRoutes\Macros\Route\isFallbackMacro;
use CodeZero\LocalizedRoutes\Macros\Route\IsLocalizedMacro;
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedMacro;
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedUrlMacro;
use CodeZero\Localizer\LocalizerServiceProvider;
use CodeZero\LocalizedRoutes\Middleware\LocaleHandler;
use CodeZero\UriTranslator\UriTranslatorServiceProvider;
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function register()
{
$this->mergeConfig();
$this->registerLocaleConfig();
$this->registerLocaleHandler();
$this->registerUrlGenerator();
$this->registerProviders();
}
Expand Down Expand Up @@ -90,7 +92,7 @@ protected function mergeConfig()
*/
protected function registerProviders()
{
$this->app->register(LocalizerServiceProvider::class);
$this->app->register(BrowserLocaleServiceProvider::class);
$this->app->register(UriTranslatorServiceProvider::class);
}

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

/**
* Register LocaleHandler.
*
* @return void
*/
protected function registerLocaleHandler()
{
$this->app->bind(LocaleHandler::class, function ($app) {
$locales = $app['locale-config']->getLocales();
$detectors = $app['config']->get("{$this->name}.detectors");
$stores = $app['config']->get("{$this->name}.stores");
$trustedDetectors = $app['config']->get("{$this->name}.trusted_detectors");

return new LocaleHandler($locales, $detectors, $stores, $trustedDetectors);
});
}

/**
* Register a custom URL generator that extends the one that comes with Laravel.
* This will override a few methods that enables us to generate localized URLs.
Expand Down
18 changes: 18 additions & 0 deletions src/Middleware/Detectors/AppDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\App;

class AppDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
return App::getLocale();
}
}
20 changes: 20 additions & 0 deletions src/Middleware/Detectors/BrowserDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use CodeZero\BrowserLocale\BrowserLocale;
use CodeZero\BrowserLocale\Filters\CombinedFilter;
use Illuminate\Support\Facades\App;

class BrowserDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
return App::make(BrowserLocale::class)->filter(new CombinedFilter);
}
}
21 changes: 21 additions & 0 deletions src/Middleware/Detectors/CookieDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;

class CookieDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$key = Config::get('localized-routes.cookie_name');

return Cookie::get($key);
}
}
13 changes: 13 additions & 0 deletions src/Middleware/Detectors/Detector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

interface Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect();
}
18 changes: 18 additions & 0 deletions src/Middleware/Detectors/OmittedLocaleDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\Config;

class OmittedLocaleDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
return Config::get('localized-routes.omitted_locale') ?: null;
}
}
21 changes: 21 additions & 0 deletions src/Middleware/Detectors/RouteActionDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;

class RouteActionDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$action = Config::get('localized-routes.route_action');

return Request::route()->getAction($action);
}
}
21 changes: 21 additions & 0 deletions src/Middleware/Detectors/SessionDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class SessionDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$key = Config::get('localized-routes.session_key');

return Session::get($key);
}
}
33 changes: 33 additions & 0 deletions src/Middleware/Detectors/UrlDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use Illuminate\Support\Facades\Request;

class UrlDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$slug = Request::segment(1);

// If supported locales is a simple array like ['en', 'nl']
// just return the slug and let the calling code check if it is supported.
if ( ! LocaleConfig::hasLocales() || LocaleConfig::hasSimpleLocales()) {
return $slug;
}

// Find the locale that belongs to the custom domain or slug.
// Return the original slug as fallback.
// The calling code should validate and handle it.
$domain = Request::getHttpHost();
$locale = LocaleConfig::findLocaleByDomain($domain) ?? LocaleConfig::findLocaleBySlug($slug) ?? $slug;

return $locale;
}
}
27 changes: 27 additions & 0 deletions src/Middleware/Detectors/UserDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

class UserDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$user = Auth::user();

if ($user === null) {
return null;
}

$attribute = Config::get('localized-routes.user_attribute');

return $user->getAttributeValue($attribute);
}
}
Loading