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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use laravel::http::{Middleware, Next, Request, Response};

use crate::locale::{config, with_locale};

/// Gives each request a locale of its own, starting at `APP_LOCALE`.
///
/// `set_locale` keeps its value in that scope, so without it a middleware or handler has nowhere
/// to store the language. The provider prepends it to the global middleware, ahead of your own,
/// and you do not add it yourself.
#[derive(Debug, Clone, Copy, Default)]
pub struct LocaleScope;

impl Middleware for LocaleScope {
    async fn handle(&self, request: Request, next: Next) -> laravel::Result<Response> {
        Ok(with_locale(config().locale, next.run(request)).await)
    }
}