Comments (2)
Thanks a lot! I've copied the SetLocale middleware and placed the controller logic in there.
public function handle($request, Closure $next)
{
// This package sets a custom route attribute for the locale.
// If it is present, use this as the locale.
$locale = $request->route()->getAction('localized-routes-locale')
?: $this->getLocaleFromFallbackRoute($request);
App::make(LocaleHandler::class)->handleLocale($locale);
//custom logic
session()->put('locale', $locale);
App::setLocale($locale);
if (auth()->check() && auth()->user()->language != $locale) {
auth()->user()->update(['language' => $locale]);
}
return $next($request);
}
Now to switch language i can simply use
<a class="dropdown-item" href="{{ Route::localizedUrl('en') }}"><i class="flag-icon flag-icon-us"></i> English</a>
<a class="dropdown-item" href="{{ Route::localizedUrl('nl') }}"><i class="flag-icon flag-icon-nl"></i> Nederlands</a>
from laravel-localized-routes.
Hello,
After some research I found the following problems:
redirect()->back()
fetches URL::previous()
, which returns the plain text URL in the referer
header of the current request (the one where you redirect()->back()
).
That plain text URL has a locale slug so that will always update the locale when you redirect to it.
Unfortunately, my code can only convert the locale and parameters of the current route or URL, because it needs a Route
object to get the parameters.
There may or may not be a way to do this, but I will need to research this further.
If possible, you could add the User
logic to the SetLocale
middleware, or create a Store
implementation in the Localizer package, if you use that. And then use a locale switcher like this for example:
@if (Route::isLocalized() || Route::isFallback())
@foreach(Config::get('localized-routes.supported-locales') as $locale)
@if ( ! App::isLocale($locale))
<li>
<a href="{{ Route::localizedUrl($locale) }}">
{{ strtoupper($locale) }}
</a>
</li>
@endif
@endforeach
@endif
from laravel-localized-routes.
Related Issues (20)
- How to use with Laravel Route Attributes
- Redirect Issue with Octane HOT 3
- Call to undefined method Composer\Util\Platform::getCwd() HOT 5
- Advanced Scoped Options - Multi-site (domain localization & slug localization) HOT 7
- Scoped Options supported_locales overridden not working
- Fallback Controller Invalid Route Action HOT 4
- Authentication Breaks on Localized Routes HOT 5
- Route not defined. HOT 3
- * Use Localizer to Detect and Set the Locale HOT 1
- Resource routes HOT 2
- composer-include-files dependency breaks autoload-dev files HOT 3
- How to override route helper? HOT 4
- Share routes with front frameworks like Inertia/VueJS HOT 4
- What's the best way to deal with more complex locales like 'ch-fr'? HOT 1
- Subdomain localized urls HOT 6
- How to remove route parameters? HOT 5
- auto redirect HOT 5
- Redirect request with default locale to omitted locale url HOT 4
- Locale Switcher HOT 2
- Call to a member function getAction() on null HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-localized-routes.