1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use RefCell;
use Future;
use Context;
use crate;
task_local!
/// The current language, such as `en`: the one `set_locale` chose for this request, command or
/// job, or else `APP_LOCALE`.
///
/// It is `laravel::lang::locale()` in Laravel Rust's own localization.
/// Sets the language of the rest of the current request, command or job, such as `es` or `pt_BR`.
///
/// Every `locale()` after it, in the handler, the views and the middleware that run later,
/// returns the new language. Concurrent requests keep their own. Nothing is validated: the
/// locale does not have to be one the application speaks.
///
/// Outside a request, which has its own scope, and outside `with_locale`, there is nowhere to
/// keep the language, so the call is ignored and a warning is logged.
///
/// It is `laravel::lang::set_locale` in Laravel Rust's own localization.
/// The fallback language, `APP_FALLBACK_LOCALE`: the one translations fall back to. It does not
/// follow the current locale.
/// The languages the application speaks: `APP_LOCALE` followed by the languages that have a
/// `{locale}.json` file in the `lang` directory, or the `supported` list of `LocaleConfig` when
/// it is set.
/// The language of the application that best matches `preferred`, a list of language tags such
/// as `es-mx`, most preferred first, or `APP_LOCALE` when none matches.
///
/// `es-MX` matches `es`, `pt` matches `pt_BR`, case and `-` versus `_` are ignored, and the
/// wildcard `*` stands for `APP_LOCALE`.
/// The languages to look in for `locale`, in order: the locale, its language without the region
/// (`pt` for `pt_BR`), and the same for `APP_FALLBACK_LOCALE`, without repeats.
///
/// `candidates("pt_BR")` is `["pt_BR", "pt", "en"]` with the default fallback.
/// Runs a future in a language, for jobs, commands and tests.
///
/// The language applies to the future and to what it awaits, and is gone when it ends. Scopes
/// nest, and a `set_locale` inside one does not leak into the one around it. A task you spawn
/// with `tokio::spawn` does not inherit it: wrap its future in `with_locale` as well.
///
/// ```ignore
/// laralocale::with_locale("es", async {
/// assert_eq!(laralocale::locale(), "es");
/// })
/// .await;
/// ```
pub async
/// A copy of the application's `LocaleConfig`, or its defaults when there is no application,
/// such as in a plain `tokio` test.
pub