mirror of
https://github.com/ankitects/anki.git
synced 2025-11-06 12:47:11 -05:00
add I18nExt trait
This commit is contained in:
parent
fcc0ee3cb7
commit
316e176d64
1 changed files with 15 additions and 9 deletions
|
|
@ -8,23 +8,29 @@ use anyhow::Result;
|
|||
use phf::phf_map;
|
||||
use phf::phf_ordered_map;
|
||||
use phf::phf_set;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Manager;
|
||||
use tauri::Runtime;
|
||||
|
||||
pub type I18n = anki_i18n::I18n<anki_i18n::Launcher>;
|
||||
pub type Tr = RwLock<Option<I18n>>;
|
||||
|
||||
pub fn setup_i18n<R: Runtime>(app: &AppHandle<R>, locales: &[&str]) {
|
||||
*app.state::<Tr>().write().expect("tr lock was poisoned!") = Some(I18n::new(locales));
|
||||
pub trait I18nExt<R: Runtime> {
|
||||
fn setup_tr(&self, locales: &[&str]);
|
||||
fn tr(&self) -> Result<I18n>;
|
||||
}
|
||||
|
||||
pub fn get_tr<R: Runtime>(app: &AppHandle<R>) -> Result<I18n> {
|
||||
let tr_state = app.state::<Tr>();
|
||||
impl<R: Runtime, T: Manager<R>> I18nExt<R> for T {
|
||||
fn setup_tr(&self, locales: &[&str]) {
|
||||
*self.state::<Tr>().write().expect("tr lock was poisoned!") = Some(I18n::new(locales));
|
||||
}
|
||||
|
||||
fn tr(&self) -> Result<I18n> {
|
||||
let tr_state = self.state::<Tr>();
|
||||
let guard = tr_state.read().expect("tr lock was poisoned!");
|
||||
guard
|
||||
.clone()
|
||||
.ok_or_else(|| anyhow!("tr was not initialised!"))
|
||||
}
|
||||
}
|
||||
|
||||
pub const LANGS: phf::OrderedMap<&'static str, &'static str> = phf_ordered_map! {
|
||||
|
|
|
|||
Loading…
Reference in a new issue