laralocale 0.1.0

The current locale for Laravel Rust, with the API of laravel::lang: locale, set_locale, locales, negotiate and preferred_locale.
Documentation
use laravel::config::{Config, Env};
use laravel::container::Registrar;
use laravel::foundation::ServiceProvider;

use crate::detect::DetectLocale;
use crate::scope::LocaleScope;

/// Wires the current locale into the application: the configuration, the scope that gives each
/// request a locale of its own and the `locale()` function in the views. `with_detection` also
/// appends `DetectLocale` to the `web` group.
///
/// ```ignore
/// Application::configure().with_providers(|providers| {
///     providers.add(LaralocaleServiceProvider::new());
/// })
/// ```
#[derive(Debug, Clone, Default)]
pub struct LaralocaleServiceProvider {
    detection: bool,
}

impl LaralocaleServiceProvider {
    /// A provider that gives each request its own locale, for your middleware to set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Also append `DetectLocale` to the `web` group, to detect the locale from the query string,
    /// the session, a cookie and the `Accept-Language` header. Without it, the application sets
    /// the locale itself, as in Laravel Rust's own localization.
    #[must_use]
    pub fn with_detection(mut self) -> Self {
        self.detection = true;

        self
    }
}

impl ServiceProvider for LaralocaleServiceProvider {
    fn config(&self, config: &mut Config, env: &Env) {
        config.discover(env!("CARGO_CRATE_NAME"), env);
    }

    fn register(&self, app: &mut Registrar) {
        app.middleware().prepend(LocaleScope);

        if self.detection {
            app.middleware().web(|web| {
                if !web.contains::<DetectLocale>() {
                    web.append(DetectLocale);
                }
            });
        }

        #[cfg(feature = "minijinja")]
        app.views().minijinja(|environment| {
            environment.add_function("locale", crate::locale);
        });
    }
}