Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tui): pick best available locale (#1337) #1388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ indexmap = { version = "2.6.0", default-features = false }
insta = "1.41.1"
ipnetwork = "0.20.0"
itertools = "0.13.0"
locale-match = "0.2.2"
maxminddb = "0.24.0"
mockall = "0.13.1"
nix = { version = "0.29.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/trippy-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ encoding_rs_io.workspace = true
etcetera.workspace = true
humantime.workspace = true
itertools.workspace = true
locale-match.workspace = true
maxminddb.workspace = true
petgraph.workspace = true
ratatui.workspace = true
Expand Down
8 changes: 7 additions & 1 deletion crates/trippy-tui/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const FALLBACK_LOCALE: &str = "en";
pub fn set_locale(locale: Option<&str>) {
if let Some(locale) = locale {
set_locale_inner(locale);
} else if let Some(locale) = sys_locale::get_locale().as_ref() {
} else if let Some(locale) = best_match() {
set_locale_inner(locale);
} else {
set_locale_inner(FALLBACK_LOCALE);
Expand Down Expand Up @@ -40,6 +40,12 @@ fn set_locale_inner(locale: &str) {
}
}

fn best_match() -> Option<&'static str> {
let available_locales = rust_i18n::available_locales!();
let user_locales = sys_locale::get_locales();
locale_match::bcp47::best_matching_locale(available_locales, user_locales)
}

fn split_locale(locale: &str) -> String {
let mut parts = locale.split(['-', '_']);
parts
Expand Down