-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from omaralalwi/feat/supportlocal-switcher-midd…
…lewares feat/ support language switcher middlewares
- Loading branch information
Showing
7 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,39 @@ | ||
<?php | ||
|
||
namespace Omaralalwi\LexiTranslate\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Omaralalwi\LexiTranslate\Traits\HasLocale; | ||
|
||
class ApiLocalized | ||
{ | ||
use HasLocale; | ||
|
||
/** | ||
* Handle an incoming API request and set the application locale. | ||
* | ||
* @param \Illuminate\Http\Request $request The incoming HTTP request. | ||
* @param \Closure $next The next middleware or handler in the pipeline. | ||
* @return \Symfony\Component\HttpFoundation\Response The processed HTTP response. | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$defaultLocal = app()->getLocale(); | ||
$headerFlag = Config::get('lexi-translate.api_locale_header_key'); | ||
$requestLocale = $request->header($headerFlag, $defaultLocal); | ||
$locale = $this->getValidatedLocale($requestLocale); | ||
|
||
if(!$locale) { | ||
$locale = app()->getLocale(); | ||
} | ||
|
||
App::setLocale($locale); | ||
$request->merge(['locale' => $locale]); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains 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,41 @@ | ||
<?php | ||
|
||
namespace Omaralalwi\LexiTranslate\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Session; | ||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Omaralalwi\LexiTranslate\Traits\HasLocale; | ||
|
||
class WebLocalized | ||
{ | ||
use HasLocale; | ||
|
||
/** | ||
* Handle an incoming request to switch the app locale. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$requestLocale = $request->route('locale') ?: $request->get('locale') ?: session('locale') ?: cookie('locale'); | ||
$locale = $this->getValidatedLocale($requestLocale); | ||
|
||
if(!$locale) { | ||
$locale = app()->getLocale(); | ||
} | ||
|
||
if ($locale && in_array($locale, Config::get('lexi-translate.supported_locales'))) { | ||
App::setLocale($locale); | ||
session(['locale' => $locale]); | ||
cookie()->queue(cookie('locale', $locale, 60 * 24 * 365)); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains 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,48 @@ | ||
<?php | ||
|
||
namespace Omaralalwi\LexiTranslate\Traits; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Omaralalwi\LexiTranslate\Enums\Language; | ||
|
||
trait HasLocale | ||
{ | ||
/** | ||
* Get the supported locales for translations. | ||
* | ||
* @return array | ||
*/ | ||
public function getSupportedLocales(): array | ||
{ | ||
return Config::get('lexi-translate.supported_locales', []); | ||
} | ||
|
||
/** | ||
* Get the validated locale. | ||
* | ||
* This function checks if the provided locale is in the list of supported locales. | ||
* If the locale is valid, it returns it; otherwise, it returns the application's default locale. | ||
* | ||
* @param string|null $locale The locale to check. | ||
* @return string The validated locale. | ||
*/ | ||
public function getValidatedLocale(?string $locale): string | ||
{ | ||
return ($locale && $this->isValidLocale($locale)) ? $locale : app()->getLocale(); | ||
} | ||
|
||
/** | ||
* Check if the given locale is valid (exists in the supported locales). | ||
* | ||
* @param string|null $locale The locale to check. | ||
* @return bool True if the locale is valid, false otherwise. | ||
*/ | ||
public function isValidLocale(?string $locale): bool | ||
{ | ||
if (!$locale) { | ||
return false; | ||
} | ||
|
||
return in_array($locale, $this->getSupportedLocales()); | ||
} | ||
} |
This file contains 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