tauri_plugin_lingua/
lib.rs

1use tauri::{
2  plugin::{Builder, TauriPlugin},
3  Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(desktop)]
9mod desktop;
10#[cfg(mobile)]
11mod mobile;
12
13mod commands;
14mod error;
15mod models;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::Lingua;
21#[cfg(mobile)]
22use mobile::Lingua;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the lingua APIs.
25pub trait LinguaExt<R: Runtime> {
26  fn lingua(&self) -> &Lingua<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::LinguaExt<R> for T {
30  fn lingua(&self) -> &Lingua<R> {
31    self.state::<Lingua<R>>().inner()
32  }
33}
34
35/// Initializes the plugin.
36pub fn init<R: Runtime>() -> TauriPlugin<R> {
37  Builder::new("lingua")
38    .invoke_handler(tauri::generate_handler![
39      commands::ping,
40      commands::create_detector_for_all_languages,
41      commands::create_detector_for_languages,
42      commands::detect_language,
43      commands::compute_language_confidence,
44      commands::compute_language_confidence_values,
45    ])
46    .setup(|app, api| {
47      #[cfg(mobile)]
48      let lingua = mobile::init(app, api)?;
49      #[cfg(desktop)]
50      let lingua = desktop::init(app, api)?;
51      app.manage(lingua);
52      Ok(())
53    })
54    .build()
55}