use crate::prelude::*; use anyhow::Result; use entity::locale::LocaleType; use log::error; use std::future::Future; use std::sync::OnceLock; static LOCALES: OnceLock = OnceLock::new(); pub struct LocaleStore { eng: Locale, ru: Locale, } impl LocaleStore { pub fn init() -> Result<()> { let ru = serde_yaml::from_slice(include_bytes!("../locales/ru.yaml"))?; let eng = serde_yaml::from_slice(include_bytes!("../locales/eng.yaml"))?; assert!( LOCALES.set(Self { eng, ru }).is_ok(), "Locales are already intialized" ); Ok(()) } } #[derive(serde::Deserialize)] pub struct Locale { pub master_password_is_not_set: Box, pub hide_button: Box, pub change_name_button: Box, pub change_login_button: Box, pub change_password_button: Box, pub delete_account_button: Box, pub couldnt_get_user_info_send_again: Box, pub unknown_command_use_help: Box, pub help_command: Box, pub no_file_send: Box, pub file_too_large: Box, pub couldnt_get_file_name: Box, pub following_accounts_have_problems: Box, pub duplicate_names: Box, pub accounts_already_in_db: Box, pub invalid_fields: Box, pub fix_that_and_send_again: Box, pub error_downloading_file: Box, pub error_getting_account_names: Box, pub error_parsing_json_file: Box, pub successfully_canceled: Box, pub invalid_password: Box, pub couldnt_get_message_text: Box, pub invalid_file_name: Box, pub account_already_exists: Box, pub master_password_too_weak: Box, pub no_lowercase: Box, pub no_uppercase: Box, pub no_numbers: Box, pub master_pass_too_short: Box, pub change_master_password_and_send_again: Box, pub wrong_master_password: Box, pub invalid_login: Box, pub start_command: Box, pub master_password_dont_match: Box, pub success: Box, pub send_master_password_again: Box, pub master_password_is_set: Box, pub send_new_master_password: Box, pub no_accounts_found: Box, pub choose_account: Box, pub couldnt_create_following_accounts: Box, pub send_master_password: Box, pub something_went_wrong: Box, pub nothing_to_cancel: Box, pub send_account_name: Box, pub send_login: Box, pub error_deleting_message: Box, pub account_not_found: Box, pub send_new_name: Box, pub send_new_login: Box, pub send_new_password: Box, pub success_choose_account_to_view: Box, pub send_json_file: Box, pub send_master_pass_to_delete_everything: Box, pub everything_was_deleted: Box, pub send_password: Box, pub send_master_pass_to_delete_account: Box, pub no_special_characters: Box, pub invalid_name: Box, pub decrypt_button: Box, pub delete_message_button: Box, pub menu_button: Box, pub choose_language: Box, word_name: Box, word_login: Box, word_password: Box, } impl Locale { pub async fn from_update(update: Update, db: Pool) -> &'static Self { let locale_type = LocaleType::locale_for_update(&update, &db).await; locale_type.get_locale() } pub fn show_account(&self, name: &str, login: &str, password: &str) -> String { format!( "{}:\n`{name}`\n{}:\n`{login}`\n{}:\n`{password}`", self.word_name, self.word_login, self.word_password ) } pub fn show_hidden_account(&self, name: &str) -> String { format!( "{}:\n`{name}`\n{}:\n\\*\\*\\*\n{}:\n\\*\\*\\*", self.word_name, self.word_login, self.word_password ) } } pub type LocaleRef = &'static Locale; pub trait LocaleTypeExt: Sized { fn locale_for_update(update: &Update, db: &Pool) -> impl Future + Send; fn from_language_code(code: &str) -> Option; fn get_locale(self) -> &'static Locale; } impl LocaleTypeExt for LocaleType { async fn locale_for_update(update: &Update, db: &Pool) -> Self { let Some(from) = update.user() else { return Self::default(); }; match Self::get_from_db(from.id.0, db).await { Ok(Some(locale)) => return locale, Ok(None) => (), Err(err) => error!("{err}"), } from.language_code .as_deref() .and_then(Self::from_language_code) .unwrap_or_default() } fn from_language_code(code: &str) -> Option { match code { "ru" => Some(Self::Ru), "en" => Some(Self::Eng), _ => None, } } fn get_locale(self) -> &'static Locale { let Some(store) = LOCALES.get() else { panic!("Locales are not initialized") }; match self { Self::Eng => &store.eng, Self::Ru => &store.ru, } } }