Last active
November 7, 2017 00:26
-
-
Save sebastiaanluca/c7cf964737fee557f8cedb4660a69e19 to your computer and use it in GitHub Desktop.
Set the Laravel app locale from any URL within a group with a fallback to the default locale.
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
<?php | |
return [ | |
'supported_locales' => [ | |
'en', | |
'fr', | |
'de', | |
], | |
]; |
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
if (! function_exists('localized_url')) { | |
/** | |
* @param string $locale | |
* @param string|null $url | |
* | |
* @return string | |
*/ | |
function localized_url(string $locale, string $url = null) : string | |
{ | |
return Localizer::getLocalizedUrl($locale, $url); | |
} | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace App\Managers; | |
use Illuminate\Http\Request; | |
class Localizer | |
{ | |
/** | |
* @var \Illuminate\Http\Request | |
*/ | |
protected $request; | |
/** | |
* @param \Illuminate\Http\Request $request | |
*/ | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* Load the application in a given locale or fall back to the default if none given. | |
* | |
* @return string|null | |
*/ | |
public function setLocale() : ?string | |
{ | |
$requestedLocale = $this->request->segment(1); | |
if (in_array($requestedLocale, supported_locales())) { | |
$locale = $requestedLocale; | |
} | |
else { | |
$locale = locale(); | |
$requestedLocale = null; | |
} | |
app()->setLocale($locale); | |
return $requestedLocale; | |
} | |
/** | |
* @param string $locale | |
* @param string|null $url | |
* | |
* @return string | |
*/ | |
public function getLocalizedUrl(string $locale, string $url = null) : string | |
{ | |
$url = $url ?? current_url(); | |
// The URL path can be empty, so make sure a default is set | |
['scheme' => $scheme, 'host' => $host, 'path' => $path] = array_merge(['path' => null], parse_url($url)); | |
$path = ! is_null($path) ? explode('/', substr($path, 1)) : []; | |
if (isset($path[0]) && in_array($path[0], supported_locales())) { | |
$path[0] = $locale; | |
} | |
else { | |
array_unshift($path, $locale); | |
} | |
return $scheme . '://' . $host . '/' . join('/', $path); | |
} | |
} |
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
use Facades\App\Managers\Localizer; | |
Route::group(['prefix' => Localizer::setLocale(), 'middleware' => 'web'], function () { | |
// Note: as we set the prefix automatically, don't include it | |
// in your routes, only the top most group. | |
Route::view('', 'pages.home'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment