Dashboard locale #2272
-
Hi! Thanks for any help provided! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
This can be done quite simply. Let's say we have a user and we want to display data in the language and time zone selected for him. Then we can create in the user model, something like the following method, which would set values for /**
* @return $this
*/
public function useSettingsAsGlobal(): self
{
date_default_timezone_set($this->timezone ?? config('app.timezone'));
config()->set('app.timezone', $this->timezone ?? config('app.timezone'));
$locale = $this->locale ?? config('app.locale');
app()->setLocale($locale);
\Carbon\Carbon::setLocale($locale);
\Carbon\CarbonInterval::setLocale($locale);
return $this;
} Then we could call it like this: app()->getLocale(); // EN
// Example apply
$user = User::first()->useSettingsAsGlobal();
app()->getLocale(); // DE I hope up to this point, everything was clear to you. Now all that's left is to apply this to each request. To do this, you can make a middleware using the artisan command: artisan make:middleware UserSettingMiddleware A new file will be created in the namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class UserSettingMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
optional($request->user())->useSettingsAsGlobal();
return $next($request);
}
} This will change the application settings for each user's settings. It remains only to determine exactly when you want to change it. If for the entire application, then you can add this to the end of the /**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
// ... Other class
\App\Http\Middleware\UserSettingMiddleware::class
], or if you want only within the Orchid then change the config file /*
|--------------------------------------------------------------------------
| Middleware
|--------------------------------------------------------------------------
|
| This middleware will be assigned to every route, giving you the
| chance to add your own middleware to this stack or override any of
| the existing middleware. Or, you can stick with this stack.
|
| You can learn more here: https://laravel.com/docs/middleware
|
*/
'middleware' => [
'public' => ['web'],
'private' => ['web', 'platform', \App\Http\Middleware\UserSettingMiddleware::class],
], Happy coding. |
Beta Was this translation helpful? Give feedback.
-
I extended @tabuna's answer a little bit (so to adhere to my needs but leaving options for futher implementations). I added a custom config property in /*
|--------------------------------------------------------------------------
| Locale
|--------------------------------------------------------------------------
|
| Set a custom locale for Orchid app
|
*/
'locale' => 'en', To be used like: config('platform.locale'); I then added a new middleware to be able to set locale also in the public part of the Orchid app (e.g. the login page) in <?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class PublicMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
app()->setLocale(config('platform.locale'));
return $next($request);
}
} Finally, I updated the middleware property in 'middleware' => [
'public' => ['web', \App\Http\Middleware\PublicMiddleware::class],
'private' => ['web', 'platform', \App\Http\Middleware\UserSettingMiddleware::class],
], This way it is possible to have a locale set for Orchid public app and a user defined locale after login. |
Beta Was this translation helpful? Give feedback.
This can be done quite simply. Let's say we have a user and we want to display data in the language and time zone selected for him.
Then we can create in the user model, something like the following method, which would set values for
PHP
andLaravel
:T…