Skip to content

Commit

Permalink
feat(locale): use system locale as default (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Feb 14, 2024
1 parent 6939399 commit 331996a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backend/tauri/src/config/verge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ impl IVerge {
pub fn template() -> Self {
Self {
clash_core: Some(ClashCore::default()),
language: match cfg!(feature = "default-meta") {
false => Some("en".into()),
true => Some("zh".into()),
language: {
let locale = crate::utils::help::get_system_locale();
Some(crate::utils::help::mapping_to_i18n_key(&locale).into())
},
#[cfg(debug_assertions)]
app_log_level: Some("debug".into()),
Expand Down
7 changes: 7 additions & 0 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ fn main() -> std::io::Result<()> {
return Ok(());
}

// Use system locale as default
let locale = {
let locale = utils::help::get_system_locale();
utils::help::mapping_to_i18n_key(&locale)
};
rust_i18n::set_locale(locale);

crate::log_err!(init::init_config());

// Panic Hook to show a panic dialog and save logs
Expand Down
12 changes: 12 additions & 0 deletions backend/tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ pub fn open_file(app: tauri::AppHandle, path: PathBuf) -> Result<()> {
Ok(())
}

pub fn get_system_locale() -> String {
tauri::api::os::locale().unwrap_or("en-US".to_string())
}

pub fn mapping_to_i18n_key(locale_key: &str) -> &'static str {
if locale_key.starts_with("zh-") {
"zh"
} else {
"en"
}
}

#[macro_export]
macro_rules! error {
($result: expr) => {
Expand Down

0 comments on commit 331996a

Please sign in to comment.