| @@ -1,0 +1,24 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Http\Middleware; | |
| 6 | + | |
| 7 | +use Metricool\Vendor\Carbon\Carbon; | |
| 8 | +use Metricool\Interfaces\MiddlewareInterface; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Set the locale for the current request. The locale is determined by the user's locale, or the site's locale if the user is not logged in. | |
| 12 | + */ | |
| 13 | +class SetLocale implements MiddlewareInterface | |
| 14 | +{ | |
| 15 | + public function handle(\WP_REST_Request $request, callable $next) | |
| 16 | + { | |
| 17 | + $locale = get_user_locale() ?: get_locale(); | |
| 18 | + switch_to_locale($locale); | |
| 19 | + | |
| 20 | + Carbon::setLocale($locale); | |
| 21 | + | |
| 22 | + return $next($request); | |
| 23 | + } | |
| 24 | +} | |