advotracker: change localization handling

* main: adapt resource path for localization information
* data/constants: Stings used in policycheck state/view
* widgets/policycheck_view: change the used localization strings
* callbacks/policycheck_state: change child ID'S to use constants and localization strings

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-09-26 17:36:27 +02:00
parent 16a538a805
commit 5b79229c00
4 changed files with 105 additions and 127 deletions

View File

@@ -1,4 +1,3 @@
//use chrono::{Local, DateTime, Duration};
use dotenv::dotenv;
use locales::t;
use orbtk::{
@@ -18,7 +17,7 @@ use serde::Deserialize;
use std::{env, process};
use std::collections::HashMap;
use std::time::{Duration, SystemTime};
use tracing::{debug, error, info, trace};
use tracing::{error, info, trace};
use crate::{
callbacks::global_state::GlobalState,
@@ -94,8 +93,7 @@ impl PolicyCheckState {
pub fn create_hashmap(&mut self, _ctx: &mut Context<'_>) -> Result<(), Box<dyn std::error::Error>> {
trace!(target: "advotracker", create_hashmap = "started");
let res = t!("policy.string.label_policy_list", self.lang);
let policy_list = PolicyList::new(res);
let policy_list = PolicyList::new("policy list");
trace!(target: "advotracker", policy_list = ?policy_list);
// create vector to hold imported data
@@ -139,13 +137,14 @@ impl PolicyCheckState {
/// Get the active language environment.
pub fn get_lang() -> String {
// get system environment
let mut lang = env::var("LANG").unwrap_or("en".to_string());
let mut lang = env::var("LANG").unwrap_or("C".to_string());
// testing environment: read from .env file
dotenv().ok();
match envy::from_env::<Environment>() {
Ok(environment) => {
if environment.test_lang != lang { lang = environment.test_lang; }
trace!(target: "advotracker", lang = ?lang);
},
Err(e) => { info!(target: "advotracker", "{}", e) }
}
@@ -157,7 +156,7 @@ impl PolicyCheckState {
-> Result<(), Box<dyn std::error::Error>> {
// WIP: for now, only import once per session
if self.policy_data_count == 0 {
TextBlock::enabled_set(&mut ctx.child("policy_check_result"), true);
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), true);
if self.policy_numbers.len() == 0 {
// initialize popup widget
@@ -216,25 +215,21 @@ impl PolicyCheckState {
ctx: &mut Context<'_>) {
trace!(target: "advotracker", parse_entry = "started");
//let policy_number_string = ctx.get_widget(policy_check_policy_number).get::<String16>("text").as_string();
let policy_number_string = TextBox::text_clone(&mut ctx.get_widget(policy_check_policy_number));
let policy_number_length = policy_number_string.len();
// // WIP: redundant lang selection (already in main!)
// let mut lang = env::var("lang").unwrap_or("en".to_string());
// // testing environment: read from .env file
// dotenv().ok();
// match envy::from_env::<Environment>() {
// Ok(environment) => {
// if environment.test_lang != lang { lang = environment.test_lang; }
// },
// Err(e) => { debug!(target: "advotracker", "{}", e); }
// }
if self.policy_data_count == 0 {
// Load data into hashmap
match self.import_data(ctx) {
Ok(()) => {
trace!(target: "advotracker", policycheck_state = "init", import_data = "success");
Stack::visibility_set(&mut ctx.child(ID_POLICY_DATA_STACK), Visibility::Visible);
let policy_data_count_string = format!("{:?}", &self.policy_data_count);
TextBlock::text_set(&mut ctx.child(ID_POLICY_DATA_COUNT), String::from(&policy_data_count_string));
},
// Load data into hashmap
match self.import_data(ctx) {
Ok(()) => trace!(target: "advotracker", policycheck_state = "init", import_data = "success"),
Err(e) => trace!(target: "advotracker", policycheck_state = "init", import_data = ?e),
Err(e) => trace!(target: "advotracker", policycheck_state = "init", import_data = ?e),
}
}
trace!(target: "advotracker", state = "parsing", policy_number = ?policy_number_string);
@@ -242,15 +237,15 @@ impl PolicyCheckState {
// Parse policy code: "AS-123456789"
// DION VERS POLLFNR
// 1 AS 1515735810
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Collapsed);
Button::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Visible);
Button::background_set(&mut ctx.child("policy_check_button_result"), String::from("transparent"));
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed);
Button::background_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("transparent"));
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Collapsed);
if policy_number_length == 10 {
// cast policy_number_sting to <u64>
match policy_number_string.parse::<u64>() {
Ok(p) => {
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(""));
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from(""));
// match hashmap's key
match self.policy_numbers.get(&p) {
@@ -260,60 +255,58 @@ impl PolicyCheckState {
policy_number = ?p, policy_code = ?policy_code);
let string_result = format!("1-{:?}-{}",
policy_code, p);
TextBlock::enabled_set(&mut ctx.child("policy_check_result"), true);
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(string_result));
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), true);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from(string_result));
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
let res = t!("policy.validation.button_success", self.lang);
Button::text_set(&mut ctx.child("policy_check_button_result"), String::from(res));
Button::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Visible);
Button::icon_set(&mut ctx.child("policy_check_button_result"), material_icons_font::MD_CHECK);
Button::icon_brush_set(&mut ctx.child("policy_check_button_result"), String::from("#008000"));
Button::foreground_set(&mut ctx.child("policy_check_button_result"), String::from("#008000"));
Button::background_set(&mut ctx.child("policy_check_button_result"), String::from("transparent"));
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Visible);
Button::icon_brush_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#008000"));
Button::foreground_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#008000"));
Button::icon_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), material_icons_font::MD_CHECK);
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
_ => {
// no matching key
let res = t!("policy.validation.failed", self.lang);
trace!(target: "advotracker", state = ?res, policy_number = ?p);
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
let res = t!("policy.validation.button_failed", self.lang);
Button::text_set(&mut ctx.child("policy_check_button_result"), String::from(res));
Button::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Visible);
Button::icon_set(&mut ctx.child("policy_check_button_result"), material_icons_font::MD_CLEAR);
Button::icon_brush_set(&mut ctx.child("policy_check_button_result"), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child("policy_check_button_result"), String::from("#FF0000"));
let res = t!("policy.validation.not_found", self.lang);
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(res));
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Visible);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from("The given policy number is invalid"));
Button::icon_brush_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::icon_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), material_icons_font::MD_CLEAR);
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
}
},
Err(e) => {
trace!(target: "advotracker", state = "error", error_type = "invalid type", error = ?e);
Button::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Visible);
Button::icon_set(&mut ctx.child("policy_check_button_result"), material_icons_font::MD_CLEAR);
Button::icon_brush_set(&mut ctx.child("policy_check_button_result"), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child("policy_check_button_result"), String::from("#FF0000"));
let res = t!("policy.validation.invalid_input", self.lang);
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(res));
TextBlock::enabled_set(&mut ctx.child("policy_check_result"), true);
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), true);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from("Only numbers are valid"));
Button::icon_brush_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::icon_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), material_icons_font::MD_CLEAR);
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
}
}
if policy_number_length < 10 {
let res = t!("policy.validation.to_short", self.lang);
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(res));
TextBlock::enabled_set(&mut ctx.child("policy_check_result"), true);
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), true);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from("Policy number is to short"));
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Visible);
Button::icon_brush_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::icon_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), material_icons_font::MD_CLEAR);
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
if policy_number_length > 10 {
let res = t!("policy.validation.to_long", self.lang);
TextBlock::text_set(&mut ctx.child("policy_check_result"), String::from(res));
TextBlock::enabled_set(&mut ctx.child("policy_check_result"), true);
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), true);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), String::from("Policy number is to long"));
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Visible);
Button::icon_brush_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::foreground_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), String::from("#FF0000"));
Button::icon_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), material_icons_font::MD_CLEAR);
Button::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
trace!(target: "advotracker", parse_entry = "finished");
@@ -342,11 +335,11 @@ impl PolicyCheckState {
/// Change status of given text box to edit mode.
fn set_entry(&mut self, text_box: Entity, ctx: &mut Context<'_>) {
if ctx.get_widget(text_box).get::<String16>("text").is_empty() {
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Collapsed);
TextBlock::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Collapsed);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Collapsed);
} else {
TextBlock::visibility_set(&mut ctx.child("policy_check_label_result"), Visibility::Visible);
TextBlock::visibility_set(&mut ctx.child("policy_check_button_result"), Visibility::Visible);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Visible);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT), Visibility::Visible);
}
}
@@ -359,13 +352,6 @@ impl PolicyCheckState {
self.menu = create_menu(current_entity, build_context);
let _menu = build_context.append_child_to_overlay(self.menu)
.expect("PolicyCheckState: Can't create overlay as child of entity");
let label_account = t!("policy.menu.label_account", self.lang);
Button::text_set(&mut ctx.child(ID_POLICY_CHECK_MENU_LABEL_ACCOUNT), String::from(label_account));
let label_quit = t!("policy.menu.label_quit", self.lang);
Button::text_set(&mut ctx.child(ID_POLICY_CHECK_MENU_LABEL_QUIT), String::from(label_quit));
let label_toggle_theme = t!("policy.menu.label_toggle_theme", self.lang);
Button::text_set(&mut ctx.child(ID_POLICY_CHECK_MENU_LABEL_TOGGLE_THEME), String::from(label_toggle_theme));
}
/// Set a progress popup that updates the import status in a progress bar
@@ -377,8 +363,7 @@ impl PolicyCheckState {
let current_entity = ctx.entity;
let build_context = &mut ctx.build_context();
let res = t!("policy.string.progress_text", self.lang);
self.progress_popup = create_popup_progress(current_entity, &res, build_context);
self.progress_popup = create_popup_progress(current_entity, build_context);
// create a progress_popup widget as a child of entity "ID_POLICY_CHECK_POLICY_NUMBER"
build_context.append_child(stack, self.progress_popup);
@@ -460,30 +445,12 @@ impl State for PolicyCheckState {
.entity_of_child(ID_POLICY_CHECK_LABEL_RESULT)
.expect("PolicyCheckState.init: Can't find resource entity 'ID_POLICY_CHECK_LABEL_RESULT'.");
// WIP: redundant lang selection (already in main!)
let mut lang = env::var("lang").unwrap_or("en".to_string());
// testing environment: read from .env file
dotenv().ok();
match envy::from_env::<Environment>() {
Ok(environment) => {
if environment.test_lang != lang { lang = environment.test_lang; }
},
Err(e) => { debug!(target: "advotracker", "{}", e); }
}
self.lang = lang;
// Constants
let res = t!("policy.string.header", self.lang);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_HEADER), String::from(res));
let res = t!("policy.string.label_policy_number", self.lang);
let string_label_policy_number = format!("{}:", res);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_POLICY_NUMBER), String::from(string_label_policy_number));
let res = t!("policy.string.label_result", self.lang);
let string_label_result = format!("{}:", res);
TextBlock::text_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), String::from(string_label_result));
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed);
// Preset localization with given environment lang
self.lang = PolicyCheckState::get_lang();
ctx.set_language(&self.lang);
// // Load the saved data from a file in 'ron' format into our data structure.
// // The cargo package identifier (default: 'nwx.advotracker') is used as the
@@ -605,23 +572,9 @@ impl State for PolicyCheckState {
self.action = None;
}
/// Update the view after the layout is rendered.
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
// WIP: redundant lang selection (already in main!)
let mut lang = env::var("lang").unwrap_or("en".to_string());
// testing environment: read from .env file
dotenv().ok();
match envy::from_env::<Environment>() {
Ok(environment) => {
if environment.test_lang != lang { lang = environment.test_lang; }
},
Err(e) => { debug!(target: "advotracker", "{}", e); }
}
let res = t!("policy.string.data_count", lang);
let string_data_count = format!("{}: {:?}", res, self.policy_numbers.len());
TextBlock::text_set(&mut ctx.child(ID_POLICY_DATA_COUNT_BLOCK), String::from(string_data_count));
}
// /// Update the view after the layout is rendered.
// fn update_post_layout(&mut self, _: &mut Registry, _ctx: &mut Context<'_>) {
// }
}
/// Create a menu popup
@@ -708,7 +661,7 @@ fn create_menu(menu: Entity, ctx: &mut BuildContext<'_>) -> Entity {
}
/// Create a progress popup with update status of an onging data import
fn create_popup_progress(target: Entity, text: &str, ctx: &mut BuildContext<'_>) -> Entity {
fn create_popup_progress(target: Entity, ctx: &mut BuildContext<'_>) -> Entity {
Popup::new()
.id(ID_POLICY_CHECK_POPUP_PROGRESS)
.target(target)
@@ -725,14 +678,15 @@ fn create_popup_progress(target: Entity, text: &str, ctx: &mut BuildContext<'_>)
.child(
Container::new()
.style("container_progress")
.child( Stack::new()
.child(
Stack::new()
.style("stack_progress")
.child(
TextBlock::new()
.id(ID_POLICY_CHECK_PROGRESS_TEXT)
//.style("textblock_progress")
.font_size(12)
.text(text)
.text("Importing data")
.build(ctx)
)
.child(
@@ -748,6 +702,7 @@ fn create_popup_progress(target: Entity, text: &str, ctx: &mut BuildContext<'_>)
//.style("textblock_progress")
.h_align("end")
.font_size(12)
.text("Processing time")
.build(ctx)
)
.build(ctx)

View File

@@ -39,12 +39,14 @@ pub static ID_POLICY_CHECK_RESULT: &'static str = "policy_check_result";
pub static ID_POLICY_CHECK_WIDGET: &'static str = "policy_check_widget";
pub static ID_POLICY_DATA_ADD_BUTTON: &'static str = "policy_data_add_button";
pub static ID_POLICY_DATA_COUNT_BLOCK: &'static str = "policy_data_count_block";
pub static ID_POLICY_DATA_COUNT: &'static str = "policy_data_count";
pub static ID_POLICY_DATA_LABEL: &'static str = "policy_data_label";
pub static ID_POLICY_DATA_ITEMS_WIDGET: &'static str = "policy_data_items_widget";
pub static ID_POLICY_DATA_DATE_INSERTED: &'static str = "policy_data_date_inserted";
pub static ID_POLICY_DATA_DION: &'static str = "policy_data_dion";
pub static ID_POLICY_DATA_POLICY_CODE: &'static str = "policy_data_policy_code";
pub static ID_POLICY_DATA_POLICY_NUMBER: &'static str = "policy_data_policy_number";
pub static ID_POLICY_DATA_STACK: &'static str = "policy_data_stack";
pub static ID_POLICY_DATA_STATUS: &'static str = "policy_data_status";
pub static ID_POLICY_DATA_LIST_NAME: &'static str = "policy_data_list_name";

View File

@@ -38,7 +38,7 @@ struct Environment {
}
// German localization file.
static ADVOTRACKER_DE_DE: &str = include_str!("../locales/advotracker_de_DE.ron");
static ADVOTRACKER_DE_DE: &str = include_str!("../resources/advotracker/advotracker_de_DE.ron");
fn main() -> Result<(), Box<dyn std::error::Error>> {
use parse_args::parse_args;

View File

@@ -43,7 +43,7 @@ impl Template for PolicyCheckView {
.v_align("center")
.child(
ImageWidget::new()
.image("resources/images/hiedemann_logo.png")
.image("resources/advotracker/hiedemann_logo.png")
.v_align("center")
.build(ctx),
)
@@ -111,7 +111,7 @@ impl Template for PolicyCheckView {
.id(ID_POLICY_CHECK_HEADER)
.v_align("center")
.h_align("left")
.text("Validierung Versicherungsnummer")
.text("Policy number validation")
.build(ctx),
)
.build(ctx),
@@ -134,7 +134,7 @@ impl Template for PolicyCheckView {
.margin((0, 0, 16, 0))
.h_align("end")
.v_align("center")
.text("Versicherungsnummer:")
.text("Policy number")
.build(ctx),
)
.child(
@@ -144,6 +144,7 @@ impl Template for PolicyCheckView {
.id(ID_POLICY_CHECK_POLICY_NUMBER)
.h_align("start")
.lost_focus_on_activation(false)
//WIP: localization for water_mark
.water_mark("10-stellig")
.on_activate(move |ctx, entity| {
// Entity is entered/activated via Mouse/Keyboard
@@ -185,6 +186,7 @@ impl Template for PolicyCheckView {
.attach(Grid::column(0))
.attach(Grid::column_span(5))
.orientation("horizontal")
//.visibility(Visibility::Collapsed)
.v_align("center")
.child(
TextBlock::new()
@@ -197,7 +199,7 @@ impl Template for PolicyCheckView {
.v_align("center")
.width(250)
.min_width(250)
.visibility(Visibility::Visible)
.text("Check result")
.build(ctx),
)
.child(
@@ -267,14 +269,33 @@ impl Template for PolicyCheckView {
//})
.build(ctx);
let policy_data_count_block = TextBlock::new()
.id(ID_POLICY_DATA_COUNT_BLOCK)
let policy_data_stack = Stack::new()
.id(ID_POLICY_DATA_STACK)
.attach(Grid::row(3))
.attach(Grid::column(1))
.margin((0, 4, 0, 0))
.h_align("end")
.v_align("top")
.enabled(true)
.orientation("horizontal")
.visibility(Visibility::Collapsed)
//.spacing("4")
.child(
TextBlock::new()
.id(ID_POLICY_DATA_LABEL)
.margin((0, 4, 0, 0))
.enabled(true)
.text("Checklist elements: ")
//.visibility(Visibility::Collapsed)
.build(ctx)
)
.child(
TextBlock::new()
.id(ID_POLICY_DATA_COUNT)
.margin((0, 4, 0, 0))
.enabled(true)
.text("0")
//.visibility(Visibility::Collapsed)
.build(ctx)
)
.build(ctx);
// Starter page: check policy numbers
@@ -310,7 +331,7 @@ impl Template for PolicyCheckView {
.child(policy_check_form)
// row 3: Sum HashMap elements
.child(policy_data_count_block)
.child(policy_data_stack)
// row 4: Policy Check Bottom
.child(policy_check_bottom_bar)