Skip to content
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

Closed
ducvu91 opened this issue Oct 29, 2024 · 3 comments
Closed

How to change default language in Middleware #912

ducvu91 opened this issue Oct 29, 2024 · 3 comments

Comments

@ducvu91
Copy link

ducvu91 commented Oct 29, 2024

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() ?

@iwasherefirst2
Copy link
Collaborator

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.

@ducvu91
Copy link
Author

ducvu91 commented Dec 2, 2024

Thanks for advice.
I have config by each tenant, and if will load after all provider load. But when provider load, it also load config in app.locale.

I'm solved my issue by this.
I created class CustomLaravelLocalization extends from LaravelLocalization

`<?php

namespace Modules\Core\Translations;

use Mcamara\LaravelLocalization\LaravelLocalization as BaseLaravelLocalization;

class CustomLaravelLocalization extends BaseLaravelLocalization
{
// Hàm setDefaultLocale() để ghi đè locale mặc định
public function setDefaultLocale($locale): void
{
$this->defaultLocale = $locale;
}
}
`
and register in provier for laravellocalization.

so, when i use, i just call for first load tenant
LaravelLocalization::setDefaultLocale($lang);
LaravelLocalization::setLocale($lang);

@iwasherefirst2
Copy link
Collaborator

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants