Skip to content

Commit

Permalink
Fix incorrect message when /setlang command is invoked without the …
Browse files Browse the repository at this point in the history
…parameter
  • Loading branch information
Leonid Kozarin committed Nov 29, 2024
1 parent 66e01bc commit 1037897
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ registration:
set-option:
language:
success: "👌🏼👍🏼"
unsupported: "Unfortunately, this language is not supported yet. It would be very nice if you contribute to our project and help us to [translate](https://github.com/kozalosev/LocPlaceBot/blob/main/locales/en.yml) the bot to your language."
empty: "Please, specify a language you want to use when invoking the command: <code>/setlang ru</code> for example."
unsupported: "Unfortunately, this language is not supported yet. It would be very nice if you contribute to our project and help us to <a href=\"https://github.com/kozalosev/LocPlaceBot/blob/main/locales/en.yml\">translate</a> the bot to your language."
location:
message:
text: "Send me your location, please"
Expand Down
1 change: 1 addition & 0 deletions locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ registration:
set-option:
language:
success: "👌🏼👍🏼"
empty: "Пожалуйста, при вызове команды укажите код языка: <code>/setlang en</code>, например."
unsupported: "К сожалению, данный язык пока не поддерживается. Было бы очень круто, если бы Вы помогли нам <a href=\"https://github.com/kozalosev/LocPlaceBot/blob/main/locales/en.yml\">перевести</a> бота на него."
location:
message:
Expand Down
35 changes: 21 additions & 14 deletions src/handlers/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::handlers::options::consent::{ConsentCallbackData, SavedSetCommand};
pub enum LanguageCode {
Ru,
En { fallback_for: Option<String> },
Empty,
}

impl FromStr for LanguageCode {
Expand All @@ -33,29 +34,35 @@ impl FromStr for LanguageCode {
match code.as_str() {
"ru" | "be" | "uk" | "🇷🇺" | "🇺🇦" | "🇧🇾" => Ok(Self::Ru),
"en" | "🇺🇸" | "🇬🇧" => Ok(Self::En { fallback_for: None }),
"" => Ok(Self::Empty),
_ => Ok(Self::En { fallback_for: Some(code) })
}
}
}

pub(super) async fn cmd_set_language_handler(usr_client: impl UserServiceClient, user: &teloxide::types::User, code: LanguageCode) -> anyhow::Result<AnswerMessage> {
if let LanguageCode::En { fallback_for } = &code {
if fallback_for.is_some() {
let requested_code = fallback_for.as_ref().unwrap();
let answer = match &code {
LanguageCode::En { fallback_for: Some(requested_code) } => {
log::warn!("unsupported language was requested: {}", requested_code);
let lang_code = &ensure_lang_code(user.id, None, &usr_client.into()).await;
return Ok(t!("set-option.language.unsupported", locale = lang_code).to_string().into())
t!("set-option.language.unsupported", locale = lang_code).to_string().into()
}
}

let code = code.to_string();
match usr_client.get(user.id).await? {
Some(_) => {
usr_client.set_language(user.id, &code).await?;
Ok(t!("set-option.language.success", locale = &code).to_string().into())
},
None => register_user(usr_client, user, SavedSetCommand::Language(code)).await
}
LanguageCode::Empty => {
let lang_code = &ensure_lang_code(user.id, None, &usr_client.into()).await;
t!("set-option.language.empty", locale = lang_code).to_string().into()
}
code => {
let code = code.to_string();
match usr_client.get(user.id).await? {
Some(_) => {
usr_client.set_language(user.id, &code).await?;
t!("set-option.language.success", locale = &code).to_string().into()
},
None => register_user(usr_client, user, SavedSetCommand::Language(code)).await?
}
}
};
Ok(answer)
}

async fn register_user(client: impl UserServiceClient, user: &teloxide::types::User, cmd: SavedSetCommand) -> anyhow::Result<AnswerMessage> {
Expand Down

0 comments on commit 1037897

Please sign in to comment.