-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
119 lines (114 loc) · 3.4 KB
/
mod.rs
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
112
113
114
115
116
117
118
119
mod default_fallback;
mod fallback_map;
mod into_language_identifier;
use bevy::prelude::*;
pub use default_fallback::LocaleDefaultFallback;
pub use fallback_map::LocaleFallbackMap;
pub use into_language_identifier::IntoLanguageIdentifier;
use unic_langid::LanguageIdentifier;
/// The currently active locale.
///
/// This struct controls which language your game currently uses.
/// It then determines which `.ftl` file is loaded for the localization.
///
/// # Creating a new [`Locale`]
///
/// The [`Locale`] should be inserted as a resource into your app:
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// #
/// App::new()
/// .insert_resource(Locale::new("en-US"))
/// .add_plugin(LocalizationPlugin)
/// // -- snip --
/// .run();
/// ```
///
/// The strings must be valid unicode language tags.
/// You can also use the [`unic_langid::langid`] macro:
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// use unic_langid::langid;
///
/// App::new()
/// .insert_resource(Locale::new(langid!("en-US")))
/// .add_plugin(LocalizationPlugin)
/// // -- snip --
/// .run();
/// ```
///
/// # Modifying the [`Locale`]
///
/// Modifying the currently active locale is as simple as editing the corresponding resource:
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// #
/// fn change_locale(mut locale: ResMut<Locale>) {
/// // Change the locale to French
/// locale.set("fr");
/// }
/// ```
///
/// Don't forget to add the system to your app.
/// This method can be easily expanded to change the locale on a button press or other user input.
#[derive(PartialEq, Eq, Debug, Clone, Resource)]
pub struct Locale(pub(crate) LanguageIdentifier);
impl Locale {
/// Create a new [`Locale`].
///
/// The [`Locale`] should be inserted as a resource into your app:
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// #
/// App::new()
/// .insert_resource(Locale::new("en-US"))
/// .add_plugin(LocalizationPlugin)
/// // -- snip --
/// .run();
/// ```
///
/// The strings must be valid unicode language tags.
/// You can also use the [`unic_langid::langid`] macro:
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// use unic_langid::langid;
///
/// App::new()
/// .insert_resource(Locale::new(langid!("en-US")))
/// .add_plugin(LocalizationPlugin)
/// // -- snip --
/// .run();
/// ```
pub fn new<T: IntoLanguageIdentifier>(locale: T) -> Self {
Locale(locale.into_language_identifier())
}
/// Change the locale.
///
/// Modifying the currently active locale is as simple as editing the corresponding resource:
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_mod_localization::prelude::*;
/// #
/// fn change_locale(mut locale: ResMut<Locale>) {
/// // Change the locale to French
/// locale.set("fr");
/// }
/// ```
///
/// Don't forget to add the system to your app.
/// This method can be easily expanded to change the locale on a button press or other user input.
pub fn set<T: IntoLanguageIdentifier>(&mut self, locale: T) {
self.0 = locale.into_language_identifier();
}
}