Skip to content

Commit

Permalink
Start pushing translations down into fitness display components
Browse files Browse the repository at this point in the history
  • Loading branch information
savannidgerinel committed Feb 10, 2020
1 parent 4df336a commit 5b31256
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 29 deletions.
69 changes: 61 additions & 8 deletions gtk/src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use fluent::{FluentBundle, FluentResource};
use std::borrow::Cow;
use fluent::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use std::fmt;
use std::sync::Arc;
use unic_langid::LanguageIdentifier;
Expand All @@ -21,6 +20,10 @@ running = Running
save = Save
situps = Situps
steps = Steps
step-count = {$count ->
[one] 1 Step
*[other] {$count} Steps
}
swimming = Swimming
timezone = Timezone
units = Units
Expand All @@ -45,11 +48,15 @@ running = Kurado
save = Ŝpari
situps = Sidiĝoj
steps = Paŝoj
step-count = {$count ->
[one] 1 Paŝo
*[other] {$count} Paŝoj
}
swimming = Naĝado
timezone = Horzono
units = Unuoj
walking = Promenadi
weight = Pezon
weight = Pezo
";

#[derive(Clone)]
Expand Down Expand Up @@ -87,25 +94,71 @@ impl Messages {
}
}

pub fn edit(&self) -> Cow<str> {
pub fn cancel(&self) -> String {
self.tr("cancel").unwrap()
}

pub fn cycling(&self) -> String {
self.tr("cycling").unwrap()
}

pub fn edit(&self) -> String {
self.tr("edit").unwrap()
}

pub fn history(&self) -> Cow<str> {
pub fn history(&self) -> String {
self.tr("history").unwrap()
}

pub fn preferences(&self) -> Cow<str> {
pub fn preferences(&self) -> String {
self.tr("preferences").unwrap()
}

pub fn tr(&self, id: &str) -> Option<Cow<str>> {
pub fn rowing(&self) -> String {
self.tr("rowing").unwrap()
}

pub fn running(&self) -> String {
self.tr("running").unwrap()
}

pub fn save(&self) -> String {
self.tr("save").unwrap()
}

pub fn step_count(&self, count: u32) -> String {
let mut _errors = vec![];

let mut args = FluentArgs::new();
args.insert("count", FluentValue::from(count));

self.bundle
.get_message("step-count")
.and_then(|msg| msg.value)
.map(move |pattern| {
String::from(
self.bundle
.format_pattern(&pattern, Some(&args), &mut _errors),
)
})
.unwrap()
}

pub fn swimming(&self) -> String {
self.tr("swimming").unwrap()
}

pub fn walking(&self) -> String {
self.tr("walking").unwrap()
}

pub fn tr(&self, id: &str) -> Option<String> {
let mut _errors = vec![];

self.bundle
.get_message(id)
.and_then(|msg| msg.value)
.map(|pattern| self.bundle.format_pattern(&pattern, None, &mut _errors))
.map(|pattern| String::from(self.bundle.format_pattern(&pattern, None, &mut _errors)))
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/components/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl SwappableDay {
let v = day_c(
&self.date,
self.records.iter().map(|rec| &rec.data).collect(),
&self.messages,
&self.prefs,
);
self.state = DayState::View(v);
Expand All @@ -119,6 +120,7 @@ impl SwappableDay {
self.state = DayState::Edit(DayEdit::new(
&self.date,
&record_map,
self.messages.clone(),
&self.prefs,
on_save,
on_cancel,
Expand Down Expand Up @@ -232,6 +234,7 @@ impl Day {
fn day_c(
_date: &chrono::Date<chrono_tz::Tz>,
data: Vec<&TraxRecord>,
messages: &Messages,
prefs: &Preferences,
) -> gtk::Box {
let container = gtk::Box::new(gtk::Orientation::Vertical, 5);
Expand All @@ -249,14 +252,16 @@ fn day_c(
for record in records {
match record {
TraxRecord::Comments(ref _rec) => (),
TraxRecord::RepDuration(ref rec) => rep_duration_components.push(rep_duration_c(&rec)),
TraxRecord::SetRep(ref rec) => set_rep_components.push(set_rep_c(&rec)),
TraxRecord::Steps(ref rec) => step_component = Some(steps_c(&rec)),
TraxRecord::RepDuration(ref rec) => {
rep_duration_components.push(rep_duration_c(&rec, &messages))
}
TraxRecord::SetRep(ref rec) => set_rep_components.push(set_rep_c(&rec, &messages)),
TraxRecord::Steps(ref rec) => step_component = Some(steps_c(&rec, &messages)),
TraxRecord::TimeDistance(ref rec) => {
time_distance_components.push(time_distance_c(&rec, &prefs))
time_distance_components.push(time_distance_c(&rec, &messages, &prefs))
}
TraxRecord::Weight(ref rec) => {
weight_component = Some(weight_record_c(&rec, &prefs.units))
weight_component = Some(weight_record_c(&rec, &messages, &prefs.units))
}
}
}
Expand Down Expand Up @@ -292,6 +297,7 @@ impl DayEdit {
fn new(
date: &chrono::Date<chrono_tz::Tz>,
data: &HashMap<UniqueId, TraxRecord>,
messages: Messages,
prefs: &Preferences,
on_save: Box<dyn Fn(Vec<(UniqueId, TraxRecord)>, Vec<TraxRecord>)>,
on_cancel: Box<dyn Fn()>,
Expand Down Expand Up @@ -385,8 +391,8 @@ impl DayEdit {
widget.pack_start(&time_distance_edit.widget, false, false, 5);

let buttons_row = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let save_button = gtk::Button::new_with_label("Save");
let cancel_button = gtk::Button::new_with_label("Cancel");
let save_button = gtk::Button::new_with_label(&messages.save());
let cancel_button = gtk::Button::new_with_label(&messages.cancel());
buttons_row.pack_start(&save_button, false, false, 5);
buttons_row.pack_start(&cancel_button, false, false, 5);
widget.pack_start(&buttons_row, false, false, 5);
Expand Down
6 changes: 5 additions & 1 deletion src/components/rep_duration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dimensioned::si::Second;
use gtk::prelude::*;

use crate::i18n::Messages;
use fitnesstrax;

fn activity_c(activity: &fitnesstrax::repduration::ActivityType) -> gtk::Label {
Expand All @@ -16,7 +17,10 @@ fn sets_c(sets: &Vec<Second<f64>>) -> gtk::Label {
gtk::Label::new(Some(&set_strs.join(" ")))
}

pub fn rep_duration_c(record: &fitnesstrax::repduration::RepDurationRecord) -> gtk::Box {
pub fn rep_duration_c(
record: &fitnesstrax::repduration::RepDurationRecord,
_messages: &Messages,
) -> gtk::Box {
let container = gtk::Box::new(gtk::Orientation::Horizontal, 5);

container.add(&activity_c(&record.activity));
Expand Down
3 changes: 2 additions & 1 deletion src/components/set_rep.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use gtk::prelude::*;

use crate::i18n::Messages;
use fitnesstrax;

fn activity_c(activity: &fitnesstrax::setrep::ActivityType) -> gtk::Label {
Expand All @@ -15,7 +16,7 @@ fn sets_c(sets: &Vec<u32>) -> gtk::Label {
gtk::Label::new(Some(&set_strs.join(" ")))
}

pub fn set_rep_c(record: &fitnesstrax::setrep::SetRepRecord) -> gtk::Box {
pub fn set_rep_c(record: &fitnesstrax::setrep::SetRepRecord, _messages: &Messages) -> gtk::Box {
let container = gtk::Box::new(gtk::Orientation::Horizontal, 5);

container.add(&activity_c(&record.activity));
Expand Down
5 changes: 3 additions & 2 deletions src/components/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use fitnesstrax::steps::StepRecord;

use crate::components::validated_text_entry_c;
use crate::errors::Error;
use crate::i18n::Messages;

pub fn steps_c(record: &fitnesstrax::steps::StepRecord) -> gtk::Label {
gtk::Label::new(Some(&format!("{} Steps", record.steps)))
pub fn steps_c(record: &fitnesstrax::steps::StepRecord, messages: &Messages) -> gtk::Label {
gtk::Label::new(Some(&messages.step_count(record.steps)))
}

pub fn steps_edit_c(
Expand Down
22 changes: 13 additions & 9 deletions src/components/time_distance_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ use std::sync::{Arc, RwLock};
use crate::components::basics::{
distance_c, distance_edit_c, duration_c, duration_edit_c, time_c, time_edit_c,
};
use crate::i18n::Messages;
use crate::preferences::{Preferences, UnitSystem};
use fitnesstrax::timedistance::{activity_types, ActivityType, TimeDistanceRecord};

fn activity_c(activity: &ActivityType) -> gtk::Label {
gtk::Label::new(match activity {
ActivityType::Cycling => Some("Cycling"),
ActivityType::Rowing => Some("Rowing"),
ActivityType::Running => Some("Running"),
ActivityType::Swimming => Some("Swimming"),
ActivityType::Walking => Some("Walking"),
})
fn activity_c(activity: &ActivityType, messages: &Messages) -> gtk::Label {
let activity_str = match activity {
ActivityType::Cycling => messages.cycling(),
ActivityType::Rowing => messages.rowing(),
ActivityType::Running => messages.running(),
ActivityType::Swimming => messages.swimming(),
ActivityType::Walking => messages.walking(),
};

gtk::Label::new(Some(&activity_str))
}

pub fn time_distance_c(
record: &fitnesstrax::timedistance::TimeDistanceRecord,
messages: &Messages,
prefs: &Preferences,
) -> gtk::Box {
let container = gtk::Box::new(gtk::Orientation::Horizontal, 5);
Expand All @@ -32,7 +36,7 @@ pub fn time_distance_c(
false,
5,
);
container.pack_start(&activity_c(&record.activity), false, false, 5);
container.pack_start(&activity_c(&record.activity, &messages), false, false, 5);
container.pack_start(
&record
.distance
Expand Down
7 changes: 6 additions & 1 deletion src/components/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use fitnesstrax::weight::WeightRecord;
use crate::components::validated_text_entry_c;
use crate::conversions::{parse_mass, render_mass};
use crate::errors::Error;
use crate::i18n::Messages;
use crate::preferences::UnitSystem;

pub fn weight_record_c(record: &WeightRecord, unit: &UnitSystem) -> gtk::Label {
pub fn weight_record_c(
record: &WeightRecord,
_messages: &Messages,
unit: &UnitSystem,
) -> gtk::Label {
gtk::Label::new(Some(&render_mass(&record.weight, unit, true)))
}

Expand Down

0 comments on commit 5b31256

Please sign in to comment.