-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
How to change default language in Middleware #912
Comments
If you are building a multi-tenant application, why are you using the same configuration for all tenants? I strongly recommend having a separate configuration per tenant. One way to achieve this is to have a dedicated clone of your repository for each tenant. This allows you to define tenant-specific settings, such as the default language, without affecting others, while still sharing a single database and storage folder across all tenants. In the LaravelLocalization package, the defaultLocale value is read directly from config('app.locale') at the time the LaravelLocalization class is initialized (see the relevant code here). Once initialized, the defaultLocale cannot be updated dynamically. Trying to make this dynamic would likely conflict with Laravel's caching mechanisms, such as config:cache, which assumes configuration values are static. If you are constrained to using the same configuration for all tenants, a possible workaround is to modify the logic in the Redirect Middleware of the LaravelLocalization package. You can override the default redirection behavior to consider the tenant's specific default language rather than relying on the globally defined defaultLocale. For instance, you could adjust the middleware logic at this point to use the tenant’s locale dynamically. However, be cautious when taking this approach. It might cause inconsistencies or conflicts with the package's internal caching mechanisms. Carefully test your application to ensure the modified behavior aligns with your requirements without introducing unintended side effects. |
Thanks for advice. I'm solved my issue by this. `<?php namespace Modules\Core\Translations; use Mcamara\LaravelLocalization\LaravelLocalization as BaseLaravelLocalization; class CustomLaravelLocalization extends BaseLaravelLocalization so, when i use, i just call for first load tenant |
Thank you for sharing your thoughts. I agree that this solution makes sense. However, as you mentioned, the locale should indeed be set in the service provider rather than the middleware to align with the intended use case. I’m glad you found a solution that works for your scenario! |
Hello,
I meet issue with default language. My site is multi tenant, each tenant has default language different. But now, i only set default language on provider, how i can set it in Middleware?.
My Case is:
my app have config default locale "en", with japan tenant, it default have ja, but when i set Config::set('app.locale', $lang); thì it not apply on Middleware. when i access domain/ja it still there, but when access domain/en it redirect to domain. it wrong beacause the lang default is ja, not en
but when i set in AppServiceProvider, it apply.
i don't know why, and i can't move logic detect tenant in to AppServiceProvider.
Pls help
==========================================
UPDATE:
i seen in code have function
public function getDefaultLocale()
{
return $this->defaultLocale;
}
$this->defaultLocale = $this->configRepository->get('app.locale');
can you add function setDefaultLocale() ?
The text was updated successfully, but these errors were encountered: