use laravel::config::{Config, Env};
use laravel::container::Registrar;
use laravel::foundation::ServiceProvider;
use crate::detect::DetectLocale;
use crate::scope::LocaleScope;
#[derive(Debug, Clone, Default)]
pub struct LaralocaleServiceProvider {
detection: bool,
}
impl LaralocaleServiceProvider {
pub fn new() -> Self {
Self::default()
}
#[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);
});
}
}