# metricool/trunk/app/Http/Middleware/SetLocale.php

Metricool – Social media and site statistics, version trunk. 25 lines.

- Page: https://pluginprobe.com/plugins/metricool/trunk/code/app/Http/Middleware/SetLocale.php
- Raw: https://pluginprobe.com/plugins/metricool/trunk/raw/app/Http/Middleware/SetLocale.php
- Modified: 2026-08-20T14:08:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/metricool/trunk/code/app/Http/Middleware/SetLocale.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace Metricool\Http\Middleware;

use Metricool\Vendor\Carbon\Carbon;
use Metricool\Interfaces\MiddlewareInterface;

/**
 * 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.
 */
class SetLocale implements MiddlewareInterface
{
    public function handle(\WP_REST_Request $request, callable $next)
    {
        $locale = get_user_locale() ?: get_locale();
        switch_to_locale($locale);

        Carbon::setLocale($locale);

        return $next($request);
    }
}

```
