diff --git a/examples/simple.rs b/examples/simple.rs index 432d74c..650f374 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -55,6 +55,13 @@ impl epi::App for ExampleApp { .weekend_days(|date| date.day() % 2 == 0), ); ui.end_row(); + + ui.label("Translation (French)"); + ui.add( + DatePicker::new("translationfrench", &mut self.date) + .translation(&translation::TRANSLATION_FRENCH), + ); + ui.end_row(); }); }); } diff --git a/src/lib.rs b/src/lib.rs index 2f34262..9216dfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,9 @@ //! //! [ex]: ./examples/simple.rs +pub mod translation; +use translation::{DateTranslation, TRANSLATION_ENGLISH}; + use std::{fmt::Display, hash::Hash}; pub use chrono::{ @@ -39,7 +42,6 @@ use eframe::{ egui, egui::{Area, Color32, DragValue, Frame, Id, Key, Order, Response, RichText, Ui, Widget}, }; -use num_traits::FromPrimitive; /// Default values of fields are: /// - sunday_first: `false` @@ -59,6 +61,7 @@ where weekend_color: Color32, weekend_func: fn(&Date) -> bool, highlight_weekend: bool, + translation: &'static DateTranslation, } impl<'a, Tz> DatePicker<'a, Tz> @@ -77,6 +80,7 @@ where weekend_color: Color32::from_rgb(196, 0, 0), weekend_func: |date| date.weekday() == Weekday::Sat || date.weekday() == Weekday::Sun, highlight_weekend: true, + translation: &TRANSLATION_ENGLISH, } } @@ -120,11 +124,19 @@ where } /// Set function, which will decide if date is a weekend day or not. + #[must_use] pub fn weekend_days(mut self, is_weekend: fn(&Date) -> bool) -> Self { self.weekend_func = is_weekend; self } + /// Set the translation of this date picker. + #[must_use] + pub fn translation(mut self, translation: &'static DateTranslation) -> Self { + self.translation = translation; + self + } + /// Draw names of week days as 7 columns of grid without calling `Ui::end_row` fn show_grid_header(&mut self, ui: &mut Ui) { let day_indexes = if self.sunday_first { @@ -133,8 +145,13 @@ where [0, 1, 2, 3, 4, 5, 6] }; for i in day_indexes { - let b = Weekday::from_u8(i).unwrap(); - ui.label(b.to_string()); + let weekday_string = self + .translation + .weekday_short + .get(i) + .copied() + .unwrap_or("Unknown"); + ui.label(weekday_string); } } @@ -199,7 +216,7 @@ where ui.horizontal(|ui| { self.show_month_control(ui); self.show_year_control(ui); - if ui.button("Today").clicked() { + if ui.button(self.translation.today).clicked() { *self.date = Utc::now().with_timezone(&self.date.timezone()).date(); } }); @@ -228,7 +245,12 @@ where /// to current date. fn show_month_control(&mut self, ui: &mut Ui) { self.date_step_button(ui, "<", Duration::days(-30)); - let month_string = chrono::Month::from_u32(self.date.month()).unwrap().name(); + let month_string = self + .translation + .month + .get(self.date.month0() as usize) + .copied() + .unwrap_or("Unknown"); // TODO: When https://github.com/emilk/egui/pull/543 is merged try to change label to combo box. ui.add(egui::Label::new( RichText::new(format!("{: <9}", month_string)).text_style(egui::TextStyle::Monospace), diff --git a/src/translation.rs b/src/translation.rs new file mode 100644 index 0000000..ec1b7ba --- /dev/null +++ b/src/translation.rs @@ -0,0 +1,47 @@ +/// A translation for strings used by egui-datepicker +pub struct DateTranslation { + /// The full-lenght twelve month of a year (starting at January) + pub month: [&'static str; 12], + /// A shortened version of the 7 days of the week (starting at Sunday) + pub weekday_short: [&'static str; 7], + /// Translation of the "today" button, that set the date to the current date when clicked + pub today: &'static str, +} + +pub const TRANSLATION_ENGLISH: DateTranslation = DateTranslation { + month: [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ], + weekday_short: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], + today: "Today", +}; + +pub const TRANSLATION_FRENCH: DateTranslation = DateTranslation { + month: [ + "janvier", + "février", + "mars", + "avril", + "mai", + "juin", + "juillet", + "août", + "septembre", + "octobre", + "novembre", + "décembre", + ], + weekday_short: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."], + today: "Aujourd'hui", +};