-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n_number.rs
86 lines (78 loc) · 2.47 KB
/
i18n_number.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
use bevy::{
ecs::{
component::{Component, ComponentHooks, StorageType},
reflect::ReflectComponent,
},
log::debug,
reflect::Reflect,
ui::widget::Text,
};
use fixed_decimal::FixedDecimal;
use super::{utils, I18nComponent};
/// Component for spawning translatable number entities that are managed by `bevy_simple_i18n`
///
/// It automatically inserts (or replaces) a Bevy `Text` component with the localized number
///
/// Updates automatically whenever the locale is changed using the [crate::resources::I18n] resource
///
/// # Example
///
/// ```
/// // Basic usage
/// world.spawn(I18nNumber::new(200.40));
///
/// // With forced locale
/// // overrides the global
/// // does not update when the locale is changed
/// world.spawn(I18nNumber::new(12051).with_locale("ja"));
/// ```
#[derive(Default, Reflect, Debug, Clone)]
#[reflect(Component)]
pub struct I18nNumber {
#[reflect(ignore)]
pub(crate) fixed_decimal: FixedDecimal,
/// Locale for this specific translation, `None` to use the global locale
pub(crate) locale: Option<String>,
}
impl I18nComponent for I18nNumber {
fn locale(&self) -> String {
self.locale
.clone()
.unwrap_or(rust_i18n::locale().to_string())
}
fn translate(&self) -> String {
utils::get_formatter(&self.locale(), &self.fixed_decimal)
.format_to_string(&self.fixed_decimal)
}
}
impl I18nNumber {
/// Creates a new `I18nNumber` component with the provided number value
pub fn new(number: impl Into<f64>) -> Self {
Self {
fixed_decimal: utils::f64_to_fd(number.into()),
locale: None,
}
}
/// Set the locale for this specific translation
pub fn with_locale(mut self, locale: impl Into<String>) -> Self {
self.locale = Some(locale.into());
self
}
}
impl Component for I18nNumber {
const STORAGE_TYPE: StorageType = StorageType::Table;
fn register_component_hooks(_hooks: &mut ComponentHooks) {
_hooks.on_add(|mut world, entity, _| {
let val = world.get::<Self>(entity).unwrap().clone();
debug!("Adding i18n number: {}", val.fixed_decimal);
if let Some(mut text) = world.get_mut::<Text>(entity) {
**text = val.translate();
} else {
world
.commands()
.entity(entity)
.insert(Text::new(val.translate()));
}
});
}
}