pub struct Translations { /* private fields */ }
Expand description
The main Resource type that stores translation data.
Implementations§
Source§impl Translations
impl Translations
pub fn new() -> Self
Sourcepub fn tr(&self, key: impl ToString) -> String
pub fn tr(&self, key: impl ToString) -> String
The short call to acquire a translation. Translations work through a key-value pair that are loaded based on the currently selected locale.
Here we specificially take a generic argument for the key such that any value that implements ToString
can be translated. This creates a decent amount of flexibility for users as they will be able to “translate” custom types if they so choose.
Sourcepub fn csv_file(&mut self, path: &Path, locale: &String) -> &mut Self
pub fn csv_file(&mut self, path: &Path, locale: &String) -> &mut Self
Modifies the current Translations data to load from a specified CSV file and load a particular locale.
Examples found in repository?
9fn main() {
10 let mut world = World::new();
11 world.insert_resource(
12 // initialize as default
13 Translations::default()
14 // select a CSV file and a default locale
15 .csv_file(&Path::new("assets/lang.csv"), &"en".into())
16 // optionally switch the current locale
17 .use_locale("es")
18 // Strips mutability to easily finish inserting into the world.
19 .build(),
20 );
21
22 let mut schedule = Schedule::default();
23 schedule.add_systems(system_use_translation);
24
25 schedule.run(&mut world);
26}
Sourcepub fn csv_raw(&mut self, csv_data: String, locale: &String) -> &mut Self
pub fn csv_raw(&mut self, csv_data: String, locale: &String) -> &mut Self
Modifies the current Translations data to load from a raw string in CSV format and load a particular locale.
Sourcepub fn data<S>(
&mut self,
locales: impl Iterator<Item = S>,
mapping: impl Iterator<Item = (S, S)>,
clear_old_data: bool,
) -> &mut Selfwhere
S: ToString,
pub fn data<S>(
&mut self,
locales: impl Iterator<Item = S>,
mapping: impl Iterator<Item = (S, S)>,
clear_old_data: bool,
) -> &mut Selfwhere
S: ToString,
Modifies the current Translations data to load from raw data. Note that using this method directly does not support changing locales. If you want that feature, you must use CSV or ODS
Sourcepub fn use_system_locale(&mut self) -> &mut Self
pub fn use_system_locale(&mut self) -> &mut Self
A convenience method for calling use_locale
with the system’s default locale.
Sourcepub fn use_locale<S>(&mut self, locale: S) -> &mut Self
pub fn use_locale<S>(&mut self, locale: S) -> &mut Self
Change the current locale to the new locale if available. Also loads the new mapping data allowing for translations to be loaded immediately.
Examples found in repository?
9fn main() {
10 let mut world = World::new();
11 world.insert_resource(
12 // initialize as default
13 Translations::default()
14 // select a CSV file and a default locale
15 .csv_file(&Path::new("assets/lang.csv"), &"en".into())
16 // optionally switch the current locale
17 .use_locale("es")
18 // Strips mutability to easily finish inserting into the world.
19 .build(),
20 );
21
22 let mut schedule = Schedule::default();
23 schedule.add_systems(system_use_translation);
24
25 schedule.run(&mut world);
26}
Sourcepub fn get_system_language() -> Option<String>
pub fn get_system_language() -> Option<String>
Returns an optional string if able to acquire the system’s current locale code. Basically just a small wrapper around bevy_device_lang
for convenience
Sourcepub fn build(&self) -> Self
pub fn build(&self) -> Self
Consumes and clones the instance to make inserting the resource into a bevy App or World a bit easier when using the builder pattern.
Examples found in repository?
9fn main() {
10 let mut world = World::new();
11 world.insert_resource(
12 // initialize as default
13 Translations::default()
14 // select a CSV file and a default locale
15 .csv_file(&Path::new("assets/lang.csv"), &"en".into())
16 // optionally switch the current locale
17 .use_locale("es")
18 // Strips mutability to easily finish inserting into the world.
19 .build(),
20 );
21
22 let mut schedule = Schedule::default();
23 schedule.add_systems(system_use_translation);
24
25 schedule.run(&mut world);
26}
Trait Implementations§
Source§impl Clone for Translations
impl Clone for Translations
Source§fn clone(&self) -> Translations
fn clone(&self) -> Translations
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Translations
impl Debug for Translations
Source§impl Default for Translations
impl Default for Translations
Source§impl PartialEq for Translations
impl PartialEq for Translations
impl Resource for Translations
impl StructuralPartialEq for Translations
Auto Trait Implementations§
impl Freeze for Translations
impl RefUnwindSafe for Translations
impl Send for Translations
impl Sync for Translations
impl Unpin for Translations
impl UnwindSafe for Translations
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.