返回 CodeWhale
localization_backend.rs
根目录 / crates / localization / src / localization_backend.rs
1 //! Populate rust-i18n from static entries with a bounded initialization stack.
2
3 use std::borrow::Cow;
4
5 type Messages = &'static [(&'static str, &'static str)];
6 include!(concat!(env!("OUT_DIR"), "/i18n_data.rs"));
7
8 pub(super) fn new() -> rust_i18n::SimpleBackend {
9 LOCALES
10 .iter()
11 .map(|(locale, entries)| {
12 (
13 Cow::Borrowed(*locale),
14 entries
15 .iter()
16 .map(|(key, value)| (Cow::Borrowed(*key), Cow::Borrowed(*value)))
17 .collect(),
18 )
19 })
20 .collect()
21 }
22
23 #[cfg(test)]
24 mod tests {
25 use super::*;
26 use rust_i18n::Backend;
27
28 #[test]
29 fn every_translation_initializes_and_matches_on_a_small_stack() {
30 std::thread::Builder::new()
31 .stack_size(256 * 1024)
32 .spawn(|| {
33 // Construct a fresh backend even if another test already
34 // initialized the global one. This catches catalog growth
35 // reintroducing an oversized initialization frame.
36 let backend = new();
37 assert_eq!(backend.available_locales().len(), LOCALES.len());
38 for (locale, entries) in LOCALES {
39 for (key, value) in *entries {
40 assert_eq!(backend.translate(locale, key).as_deref(), Some(*value));
41 }
42 }
43 })
44 .expect("spawn bounded-stack translation test")
45 .join()
46 .expect("translation initialization must not overflow");
47 }
48 }
49
49 lines RUST