Skip to content

Commit c0ebd75

Browse files
committed
feat: select locale on competitor card scan
1 parent 5d3bd92 commit c0ebd75

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

src/rfid.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ async fn process_card_info_response(
168168
state.competitor_display = Some(resp.display);
169169
state.current_competitor = Some(resp.card_id);
170170

171+
let competitor_locale =
172+
crate::translations::get_locale_index(&resp.country_iso2.to_lowercase());
173+
if competitor_locale != crate::translations::current_locale_index() {
174+
crate::translations::select_locale_idx(competitor_locale, global_state);
175+
}
176+
171177
match resp.possible_groups.len() {
172178
1 => {
173179
state.solve_group = Some(resp.possible_groups[0].clone());

src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ impl SignaledGlobalStateInner {
275275
if let Some(nvs) = save_nvs {
276276
SavedGlobalState::clear_saved_global_state(nvs).await;
277277
}
278+
279+
crate::translations::restore_default_locale();
278280
}
279281

280282
pub fn to_saved_global_state(&self) -> Option<SavedGlobalState> {

src/translations.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct LocalLocale {
1414
}
1515

1616
static mut SELECTED_LOCALE: usize = usize::MAX;
17+
static mut DEFAULT_LOCALE: usize = usize::MAX;
1718
pub static LOCALES: Mutex<CriticalSectionRawMutex, Vec<LocalLocale>> = Mutex::new(Vec::new());
1819
macros::load_default_translations!("src/default_translation.json");
1920

@@ -25,27 +26,58 @@ pub fn clear_locales() {
2526
}
2627

2728
pub fn select_locale(locale: &str, global_state: &GlobalState) {
29+
let locale = locale.to_lowercase();
30+
let locale_idx = get_locale_index(&locale);
31+
select_locale_idx(locale_idx, global_state);
32+
33+
log::info!("Selected locale: {locale}");
34+
}
35+
36+
pub fn select_locale_idx(mut locale_idx: usize, global_state: &GlobalState) {
37+
unsafe {
38+
if locale_idx == SELECTED_LOCALE {
39+
return;
40+
}
41+
42+
if locale_idx == usize::MAX {
43+
locale_idx = DEFAULT_LOCALE;
44+
}
45+
46+
SELECTED_LOCALE = locale_idx;
47+
global_state.state.signal(); // reload locale
48+
}
49+
}
50+
51+
pub fn set_default_locale() {
52+
unsafe {
53+
DEFAULT_LOCALE = SELECTED_LOCALE;
54+
}
55+
}
56+
57+
pub fn restore_default_locale() {
58+
unsafe {
59+
SELECTED_LOCALE = DEFAULT_LOCALE;
60+
}
61+
}
62+
63+
pub fn get_locale_index(locale: &str) -> usize {
2864
if let Ok(t) = LOCALES.try_lock() {
29-
let locale_idx = t
30-
.iter()
65+
t.iter()
3166
.enumerate()
3267
.find(|(_, l)| l.locale == locale)
3368
.map(|(i, _)| i)
34-
.unwrap_or(usize::MAX);
35-
36-
unsafe {
37-
if locale_idx == SELECTED_LOCALE {
38-
return;
39-
}
40-
41-
SELECTED_LOCALE = locale_idx;
42-
global_state.state.signal(); // reload locale
43-
log::info!("Selected locale: {locale}");
44-
}
69+
.unwrap_or(usize::MAX)
70+
} else {
71+
usize::MAX
4572
}
4673
}
4774

75+
pub fn current_locale_index() -> usize {
76+
unsafe { SELECTED_LOCALE }
77+
}
78+
4879
pub fn process_locale(locale: String, records: Vec<TranslationRecord>) {
80+
let locale = locale.to_lowercase();
4981
if let Ok(mut t) = LOCALES.try_lock() {
5082
let tmp_locale = match t.iter_mut().find(|l| l.locale == locale) {
5183
Some(tmp_locale) => tmp_locale,

src/ws.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ async fn ws_rw(
273273
}
274274

275275
crate::translations::select_locale(&default_locale, &global_state);
276+
crate::translations::set_default_locale();
276277
}
277278
TimerPacketInner::ApiError(e) => {
278279
// if should_reset_time reset time

0 commit comments

Comments
 (0)