fix: prefer terminal locale

This commit is contained in:
iff 2025-06-02 14:17:28 +02:00
parent 5a64e6c6c8
commit 73fa43d761
3 changed files with 29 additions and 7 deletions

View file

@ -64,7 +64,18 @@ fn main() -> Result<(), std::io::Error> {
fn init() -> Result<shell::Data, args::Status> { fn init() -> Result<shell::Data, args::Status> {
let locale = { let locale = {
let sys_locale = get_locale().unwrap_or("en-US".to_string()); let sys_locale = {
// use terminal locale if available
if let Ok(locale) = env::var("LANG") {
locale
} else if let Ok(locale) = env::var("LC_ALL") {
locale
} else if let Ok(locale) = env::var("LC_MESSAGES") {
locale
} else {
get_locale().unwrap_or("en-US".to_string())
}
};
if sys_locale.len() < 2 { if sys_locale.len() < 2 {
"en-US".to_string() "en-US".to_string()
} else { } else {

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::env;
use crate::requests::ai_suggestion; use crate::requests::ai_suggestion;
use sys_locale::get_locale; use sys_locale::get_locale;
mod buffer; mod buffer;
@ -36,7 +38,18 @@ async fn main() -> Result<(), std::io::Error> {
} }
let locale = { let locale = {
let sys_locale = get_locale().unwrap_or("en-US".to_string()); let sys_locale = {
// use terminal locale if available
if let Ok(locale) = env::var("LANG") {
locale
} else if let Ok(locale) = env::var("LC_ALL") {
locale
} else if let Ok(locale) = env::var("LC_MESSAGES") {
locale
} else {
get_locale().unwrap_or("en-US".to_string())
}
};
if sys_locale.len() < 2 { if sys_locale.len() < 2 {
"en-US".to_string() "en-US".to_string()
} else { } else {
@ -60,7 +73,7 @@ async fn main() -> Result<(), std::io::Error> {
if command.split_whitespace().count() == 1 { if command.split_whitespace().count() == 1 {
return Ok(()); return Ok(());
} }
ai_suggestion(&command, &error).await; ai_suggestion(&command, &error, &locale).await;
Ok(()) Ok(())
} }

View file

@ -1,8 +1,6 @@
use askama::Template; use askama::Template;
use std::collections::HashMap; use std::collections::HashMap;
use sys_locale::get_locale;
use futures_util::StreamExt; use futures_util::StreamExt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -56,7 +54,7 @@ struct AiPrompt<'a> {
set_locale: &'a str, set_locale: &'a str,
} }
pub async fn ai_suggestion(last_command: &str, error_msg: &str) { pub async fn ai_suggestion(last_command: &str, error_msg: &str, locale: &str) {
let conf = match Conf::new() { let conf = match Conf::new() {
Some(conf) => conf, Some(conf) => conf,
None => { None => {
@ -76,7 +74,7 @@ pub async fn ai_suggestion(last_command: &str, error_msg: &str) {
let user_locale = { let user_locale = {
let locale = std::env::var("_PR_AI_LOCALE") let locale = std::env::var("_PR_AI_LOCALE")
.unwrap_or_else(|_| get_locale().unwrap_or("en-us".to_string())); .unwrap_or_else(|_| locale.to_string());
if locale.len() < 2 { if locale.len() < 2 {
"en-US".to_string() "en-US".to_string()
} else { } else {