Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: desktop get locale/dialect from Tauri (#3509)

Andrew Dunbard05fdec2d2a4
diff --git a/harper-core/src/dict_word_metadata.rs b/harper-core/src/dict_word_metadata.rs
index 6864f6d28..99af969cd 100644
--- a/harper-core/src/dict_word_metadata.rs
+++ b/harper-core/src/dict_word_metadata.rs
@@ -1088,6 +1088,10 @@ impl Dialect {
_ => None,
}
}
+ // BCP-47 https://www.rfc-editor.org/rfc/rfc5646
+ pub fn try_from_bcp47(bcp47: &str) -> Option<Self> {
+ bcp47.strip_prefix("en-").and_then(Self::try_from_abbr)
+ }
}
impl TryFrom<DialectFlags> for Dialect {
type Error = ();
diff --git a/harper-desktop/src-tauri/Cargo.toml b/harper-desktop/src-tauri/Cargo.toml
index 2f9befc29..f60fa415e 100644
--- a/harper-desktop/src-tauri/Cargo.toml
+++ b/harper-desktop/src-tauri/Cargo.toml
@@ -34,6 +34,7 @@ harper-core = { path = "../../harper-core" }
harper-dictionary-wordlist = { path = "../../harper-dictionary-wordlist" }
egui_commonmark = "0.23.0"
tokio = { version = "1.52.2", features = ["full"] }
+tauri-plugin-os = "2.3.2"
[target.'cfg(target_os = "macos")'.dependencies]
accessibility = "0.2.0"
diff --git a/harper-desktop/src-tauri/src/config.rs b/harper-desktop/src-tauri/src/config.rs
index 4bf83c67d..81fefa239 100644
--- a/harper-desktop/src-tauri/src/config.rs
+++ b/harper-desktop/src-tauri/src/config.rs
@@ -44,7 +44,7 @@ impl Config {
pub fn new() -> Self {
Self {
mutable_dictionary: MutableDictionary::new(),
- dialect: Dialect::American,
+ dialect: Self::detect_system_dialect(),
ignored_lints: IgnoredLints::new(),
lint_config: FlatConfig::new_curated(),
integrations: Self::curated_integrations(),
@@ -55,6 +55,12 @@ impl Config {
}
}
+ pub fn detect_system_dialect() -> Dialect {
+ tauri_plugin_os::locale()
+ .and_then(|bcp47| Dialect::try_from_bcp47(&bcp47))
+ .unwrap_or(Dialect::American)
+ }
+
pub fn curated_integrations() -> Vec<Integration> {
[
"com.apple.TextEdit",
diff --git a/harper-desktop/src-tauri/src/lib.rs b/harper-desktop/src-tauri/src/lib.rs
index 1f6d32d50..4ca1c0b59 100644
--- a/harper-desktop/src-tauri/src/lib.rs
+++ b/harper-desktop/src-tauri/src/lib.rs
@@ -554,6 +554,7 @@ pub fn run_tauri() {
None,
))
.plugin(tauri_plugin_opener::init())
+ .plugin(tauri_plugin_os::init())
.invoke_handler(tauri::generate_handler![
get_lint_config,
get_dialect,