From f5071fb630faa4d1ffea3052193bbe043d6ec68a Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Thu, 27 Jul 2023 23:22:15 +0200 Subject: [PATCH] print: Make units translatable Closes #142 Part-of: --- src/widgets/print.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/widgets/print.rs b/src/widgets/print.rs index 272ff26..b1f0fa4 100644 --- a/src/widgets/print.rs +++ b/src/widgets/print.rs @@ -140,6 +140,22 @@ impl> From for Unit { } } +impl std::fmt::Display for Unit { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + let s = match self { + // Translators: Shorthand for centimeter + Self::Centimeter => gettext("cm"), + // Translators: Shorthand for inch + Self::Inch => gettext("in"), + // Translators: Shorthand for pixel + Self::Pixel => gettext("px"), + Self::Percent => String::from("%"), + }; + + f.write_str(&s) + } +} + impl Unit { fn factor(&self, dpi: f64, total_size: i32) -> f64 { match self { @@ -173,6 +189,13 @@ impl Unit { let f = 10_f64.powi(self.digits() as i32); (num * f).round() / f } + + fn expression() -> gtk::ClosureExpression { + gtk::ClosureExpression::new::( + [] as [gtk::Expression; 0], + glib::closure!(|s: gtk::StringObject| Self::from(s.string().as_str()).to_string()), + ) + } } mod imp { @@ -283,6 +306,7 @@ mod imp { self.margin_unit.connect_selected_notify( glib::clone!(@weak obj => move |_| obj.on_margin_unit_changed()), ); + self.margin_unit.set_expression(Some(Unit::expression())); self.margin_horizontal.connect_value_notify( glib::clone!(@weak obj => move |_| obj.on_margin_horizontal_changed()), ); @@ -294,6 +318,7 @@ mod imp { self.size_unit.connect_selected_notify( glib::clone!(@weak obj => move |_| obj.on_size_unit_changed()), ); + self.size_unit.set_expression(Some(Unit::expression())); self.fill_space.connect_active_notify( glib::clone!(@weak obj => move |_| obj.on_fill_space_changed()), );