<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Util;

defined( 'ABSPATH' ) || exit;

class LocaleUtils
{
    /**
     * Execute a callback with a temporarily switched WordPress locale.
     * Guarantees restore_current_locale() even if the callback throws.
     */
    public static function withLocale(string $locale, callable $callback): mixed
    {
        switch_to_locale($locale);
        try {
            return $callback();
        } finally {
            restore_current_locale();
        }
    }
}
