-
Notifications
You must be signed in to change notification settings - Fork 296
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
Carbon localization not set via App::setLocale() #551
Carbon localization not set via App::setLocale() #551
Comments
This is an expected problem, because of how Laravel Octane works. Laravel Octane is instantiated only once, so carbon is also instantiated only once, even if you call it several times in different requests, therefore, it will always get the default language it was instantiated with. But be careful, if for example, it is instantiated by the default language In the case of languages, Laravel (at least in version 9) dispatch the You could listen to this event, for each time the language is changed in your application, update it also in carbon and in other places where you need to update it. EDIT: Gets the application from when laravel octane is instantiated, so even if the language is changed, it will continue to return the default language, causing carbon to not update. A temporary solution to this problem until it is fixed, is to create your own service provider: namespace App\Providers;
use Carbon\Laravel\ServiceProvider;
use Illuminate\Container\Container;
class CarbonServiceProvider extends ServiceProvider
{
public function updateLocale()
{
$this->app = Container::getInstance();
parent::updateLocale();
}
} And disable the original provider in
|
Thanks for the explanation and the workaround. |
Thanks for the report @Cluster2a and for the solution @mcolominas. Looks like this indeed needs to be fixed within Carbon itself. |
We can work around this in Carbon, I think about something like this: So we don't break support for existing application that might need events to be bound to @driesvints Can you explain why in the first place If |
I'm probably not the right person to answer this. @nunomaduro might shed some more light here. |
@kylekatarnls Here he explains a little how octane works Dependency Injection & Octane |
Please consider the idea exposed in #556 |
Description:
I am using a localization middleware that loads the user language and sets the App localization:
Setting the localization of the App will result in heaving localized Carbon dates (Monday / Montag), as long as I run the App using NGINX Unit.
If I switch to Octane this code above is not working properly, as Carbon is always using the default language (en), instead of the App language (de). I additionally need to call Carbon::setlocale() to fix this behaviour:
I am not sure why Carbon behaves differently on NGINX Unit / Octane. This might be a bug - I am not sure.
I would assume that the code (without
Carbon::setlocale($userLang);
) behaves in the same way on both servers.Steps To Reproduce:
The text was updated successfully, but these errors were encountered: