fix(settings): map unknown UI locales to en-US for commit prompts

Portuguese and other interface languages have no commit prompt catalog; sending pt-BR made settings fail to deserialize.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
leokun
2026-09-15 21:31:00 +08:00
co-authored by Cursor
parent c0d5f9a815
commit 127cf8b883
5 changed files with 56 additions and 12 deletions
+6 -5
View File
@@ -149,13 +149,11 @@ pub async fn update_pricing_settings(
}
fn requested_commit_locale(headers: &HeaderMap) -> CommitPromptLocale {
match headers
headers
.get(header::ACCEPT_LANGUAGE)
.and_then(|value| value.to_str().ok())
{
Some(value) if value.eq_ignore_ascii_case("zh-CN") => CommitPromptLocale::ZhCn,
_ => CommitPromptLocale::EnUs,
}
.map(CommitPromptLocale::from_interface_language)
.unwrap_or(CommitPromptLocale::EnUs)
}
#[cfg(test)]
@@ -170,5 +168,8 @@ mod tests {
headers.insert(header::ACCEPT_LANGUAGE, "en-US".parse().unwrap());
assert_eq!(requested_commit_locale(&headers), CommitPromptLocale::EnUs);
headers.insert(header::ACCEPT_LANGUAGE, "pt-BR".parse().unwrap());
assert_eq!(requested_commit_locale(&headers), CommitPromptLocale::EnUs);
}
}
+37 -2
View File
@@ -1,5 +1,5 @@
//! Persists application settings.
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use crate::Result;
@@ -105,7 +105,7 @@ impl TabSettings {
}
}
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub enum CommitPromptLocale {
#[default]
#[serde(rename = "zh-CN")]
@@ -115,6 +115,14 @@ pub enum CommitPromptLocale {
}
impl CommitPromptLocale {
pub fn from_interface_language(value: &str) -> Self {
if value.eq_ignore_ascii_case("zh-CN") {
Self::ZhCn
} else {
Self::EnUs
}
}
pub fn default_prompt(self) -> &'static str {
match self {
Self::ZhCn => DEFAULT_COMMIT_PROMPT_ZH_CN.trim(),
@@ -123,6 +131,13 @@ impl CommitPromptLocale {
}
}
impl<'de> Deserialize<'de> for CommitPromptLocale {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = String::deserialize(deserializer)?;
Ok(Self::from_interface_language(&value))
}
}
/// User preferences for Git commit message generation.
///
/// Empty `model_id` means 直连: forward the original Cursor RPC unchanged.
@@ -490,6 +505,26 @@ mod tests {
const LEGACY_PROXY_ROW: &str =
r#"{"mode":"system","address":"","auth_enabled":false,"username":"","password":""}"#;
#[test]
fn commit_prompt_locale_maps_unknown_interface_languages_to_english() {
assert_eq!(
serde_json::from_str::<CommitPromptLocale>(r#""zh-CN""#).unwrap(),
CommitPromptLocale::ZhCn
);
assert_eq!(
serde_json::from_str::<CommitPromptLocale>(r#""en-US""#).unwrap(),
CommitPromptLocale::EnUs
);
assert_eq!(
serde_json::from_str::<CommitPromptLocale>(r#""pt-BR""#).unwrap(),
CommitPromptLocale::EnUs
);
assert_eq!(
serde_json::to_string(&CommitPromptLocale::EnUs).unwrap(),
r#""en-US""#
);
}
#[test]
fn default_commit_prompt_follows_its_saved_locale() {
for (prompt_locale, expected) in [