Skip to content

Commit 2337265

Browse files
committed
feat: index app names in system language
1 parent f5b33af commit 2337265

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

src-tauri/Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ cfg-if = "1.0.1"
107107
sysinfo = "0.35.2"
108108
indexmap = { version = "2.10.0", features = ["serde"] }
109109
strum = { version = "0.27.2", features = ["derive"] }
110+
sys-locale = "0.3.2"
110111

111112
[target."cfg(target_os = \"macos\")".dependencies]
112113
tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2" }

src-tauri/src/extension/built_in/application/with_feature.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use tokio::sync::oneshot::Sender as OneshotSender;
4040
// have to keep it.
4141
const FIELD_APP_NAME: &str = "app_name";
4242

43+
const FIELD_APP_NAME_IN_SYSTEM_LANG: &str = "app_name_in_system_lang";
4344
const FIELD_APP_NAME_ZH: &str = "app_name_zh";
4445
const FIELD_APP_NAME_EN: &str = "app_name_en";
4546
const FIELD_ICON_PATH: &str = "icon_path";
@@ -131,6 +132,19 @@ async fn get_app_name_en(app: &App) -> String {
131132
app.name.clone()
132133
}
133134

135+
/// Helper function to return `app`'s name in system language.
136+
async fn get_app_name_in_system_lang(app: &App) -> String {
137+
let system_lang = crate::util::system_lang::get_system_lang();
138+
139+
// First try en_US
140+
if let Some(name) = app.localized_app_names.get(&system_lang) {
141+
name.clone()
142+
} else {
143+
// Fall back to base name
144+
app.name.clone()
145+
}
146+
}
147+
134148
/// Helper function to return an absolute path to `app`'s icon.
135149
///
136150
/// On macOS/Windows, we cache icons in our data directory using the `icon()` function.
@@ -232,6 +246,10 @@ async fn index_applications_if_not_indexed(
232246
schema
233247
.add_property(FIELD_APP_NAME_EN, field_app_name_en)
234248
.expect("no collision could happen");
249+
let field_app_name_in_system_lang = Property::builder(FieldType::Text).build();
250+
schema
251+
.add_property(FIELD_APP_NAME_IN_SYSTEM_LANG, field_app_name_in_system_lang)
252+
.expect("no collision could happen");
235253
let property_icon = Property::builder(FieldType::Text).index(false).build();
236254
schema
237255
.add_property(FIELD_ICON_PATH, property_icon)
@@ -277,13 +295,17 @@ async fn index_applications_if_not_indexed(
277295
let app_path = get_app_path(app);
278296
let app_name_zh = get_app_name_zh(app).await;
279297
let app_name_en = get_app_name_en(app).await;
298+
let app_name_in_system_lang = get_app_name_in_system_lang(app).await;
280299
let app_icon_path = get_app_icon_path(&tauri_app_handle, app)
281300
.await
282301
.map_err(|str| anyhow::anyhow!(str))?;
283302
let app_alias = get_app_alias(&tauri_app_handle, &app_path).unwrap_or(String::new());
284303

285-
// Skip if both names are empty
286-
if app_name_zh.is_empty() && app_name_en.is_empty() {
304+
// Skip if all names are empty
305+
if app_name_zh.is_empty()
306+
&& app_name_en.is_empty()
307+
&& app_name_in_system_lang.is_empty()
308+
{
287309
continue;
288310
}
289311

@@ -298,10 +320,12 @@ async fn index_applications_if_not_indexed(
298320
// You cannot write `app_name.clone()` within the `doc!()` macro, we should fix this.
299321
let app_name_zh_clone = app_name_zh.clone();
300322
let app_name_en_clone = app_name_en.clone();
323+
let app_name_in_system_lang = app_name_in_system_lang.clone();
301324
let app_path_clone = app_path.clone();
302325
let document = doc!( app_path_clone, {
303326
FIELD_APP_NAME_ZH => app_name_zh_clone,
304327
FIELD_APP_NAME_EN => app_name_en_clone,
328+
FIELD_APP_NAME_IN_SYSTEM_LANG => app_name_in_system_lang,
305329
FIELD_ICON_PATH => app_icon_path,
306330
FIELD_APP_ALIAS => app_alias,
307331
}
@@ -444,7 +468,9 @@ impl Task for SearchApplicationsTask {
444468
// In order to be backward compatible, we still do match and prefix queries to the
445469
// app_name field.
446470
let dsl = format!(
447-
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}",
471+
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_ZH}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_EN}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME_IN_SYSTEM_LANG}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME_IN_SYSTEM_LANG}\": {:?} }} }}, {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}",
472+
self.query_string,
473+
self.query_string,
448474
self.query_string,
449475
self.query_string,
450476
self.query_string,

src-tauri/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub(crate) mod app_lang;
22
pub(crate) mod file;
33
pub(crate) mod platform;
4+
pub(crate) mod system_lang;
45
pub(crate) mod updater;
56

67
use std::{path::Path, process::Command};

src-tauri/src/util/system_lang.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use sys_locale::get_locale;
2+
3+
/// Helper function to get the system language.
4+
///
5+
/// We cannot return `enum Lang` here because Coco has limited language support
6+
/// but the OS supports many more languages.
7+
pub(crate) fn get_system_lang() -> String {
8+
// fall back to English (general) when we cannot get the locale
9+
//
10+
// We replace '-' with '_' in applications-rs, to make the locales match,
11+
// we need to do this here as well.
12+
get_locale().unwrap_or("en".into()).replace('-', "_")
13+
}

0 commit comments

Comments
 (0)