Skip to content

Commit

Permalink
Display local language name in the selector
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jul 15, 2024
1 parent 7eb48d3 commit 57b9c53
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 1,037 deletions.
10 changes: 10 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 @@ -44,6 +44,7 @@ egui_extras = { version = "0.28", features = ["svg"] }
enigo = { version = "0.2" }
futures = { version = "0.3" }
global-hotkey = { version = "0.5" }
language = { version = "0.1", features = ["serde"] }
parking_lot = { version = "0.12" }
reqwew = { version = "0.2" }
rodio = { version = "0.19" }
Expand Down
6 changes: 3 additions & 3 deletions src/component/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl ComboBoxItem for Function {
[Self::Rewrite, Self::Translate]
}

fn as_str(&self) -> &'static str {
match self {
fn as_str(&self) -> Cow<str> {
Cow::Borrowed(match self {
Self::Rewrite | Self::RewriteDirectly => "Rewrite",
Self::Translate | Self::TranslateDirectly => "Translate",
}
})
}
}
8 changes: 5 additions & 3 deletions src/component/openai.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// std
use std::borrow::Cow;
// crates.io
use async_openai::{
config::OpenAIConfig,
Expand Down Expand Up @@ -82,10 +84,10 @@ impl ComboBoxItem for Model {
[Self::Gpt4o, Self::Gpt35Turbo]
}

fn as_str(&self) -> &'static str {
match self {
fn as_str(&self) -> Cow<str> {
Cow::Borrowed(match self {
Self::Gpt4o => "gpt-4o",
Self::Gpt35Turbo => "gpt-3.5-turbo",
}
})
}
}
13 changes: 8 additions & 5 deletions src/component/setting.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod language;
use language::Language;
// A fallback for users who use older settings.
mod fallback;

// std
use std::{borrow::Cow, fs, path::PathBuf};
// crates.io
use app_dirs2::AppDataType;
use async_openai::config::OPENAI_API_BASE;
use language::Language;
use serde::{Deserialize, Serialize};
use tracing::Level;
// self
Expand Down Expand Up @@ -131,7 +132,9 @@ impl Rewrite {
#[serde(rename_all = "kebab-case")]
pub struct Translation {
pub additional_prompt: String,
#[serde(deserialize_with = "fallback::translation_a")]
pub a: Language,
#[serde(deserialize_with = "fallback::translation_b")]
pub b: Language,
}
impl Translation {
Expand Down Expand Up @@ -223,13 +226,13 @@ impl ComboBoxItem for LogLevel {
[Self::Trace, Self::Debug, Self::Info, Self::Warn, Self::Error]
}

fn as_str(&self) -> &'static str {
match self {
fn as_str(&self) -> Cow<str> {
Cow::Borrowed(match self {
Self::Trace => "Trace",
Self::Debug => "Debug",
Self::Info => "Info",
Self::Warn => "Warn",
Self::Error => "Error",
}
})
}
}
17 changes: 17 additions & 0 deletions src/component/setting/fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// crates.io
use language::Language;
use serde::{Deserialize, Deserializer};

pub fn translation_a<'de, D>(d: D) -> Result<Language, D::Error>
where
D: Deserializer<'de>,
{
Ok(Language::deserialize(d).unwrap_or(Language::ZhCn))
}

pub fn translation_b<'de, D>(d: D) -> Result<Language, D::Error>
where
D: Deserializer<'de>,
{
Ok(Language::deserialize(d).unwrap_or(Language::EnGb))
}
Loading

0 comments on commit 57b9c53

Please sign in to comment.