i18n.rs (5527B)
1 use std::fs; 2 3 #[derive(Clone)] 4 pub struct ReportMsgs { 5 pub msg: String, 6 pub error_msg: String, 7 pub success_msg: String, 8 pub cancel_msg: String, 9 } 10 11 #[derive(Clone)] 12 pub struct I18n { 13 lang: String, 14 pub start_msg: String, 15 pub song_not_found: String, 16 pub report: ReportMsgs, 17 } 18 19 impl I18n { 20 pub fn new(lang: String, songs_path: String) -> Self { 21 match lang.as_str() { 22 "de" => Self { 23 lang, 24 start_msg: String::from(format!( 25 "Hallo. Dies ist ein digitales Liederbuch. :)\n\ 26 Befehle:\n\ 27 /list - Listet alle Lieder auf\n\ 28 {}\ 29 Ansonsten tippe einfach den Titel oder Teile des Titels \ 30 des Liedes ein und du bekommst dein Lied zugeschickt.", 31 get_commands(songs_path).as_str() 32 )), 33 song_not_found: String::from("Kein Lied mit diesem Titel gefunden."), 34 report: ReportMsgs { 35 msg: String::from( 36 "Bitte sende einen gefundenen Fehler \ 37 entweder als Text oder Sprachnachricht.", 38 ), 39 error_msg: String::from( 40 "Das hat nicht funktioniert. Versuche es nochmal oder /cancel.", 41 ), 42 success_msg: String::from("Deine Korrektur wurde erfolgreich gemeldet."), 43 cancel_msg: String::from("Das Fehlermelden wurde abgebrochen."), 44 }, 45 }, 46 "ro" | "md" => Self { 47 lang, 48 start_msg: String::from(format!( 49 "Salut! Această e o carte de cântari digitală. :)\n\ 50 Comenzi:\n\ 51 /list - Listează toate cântările\n\ 52 {}\ 53 Deasemenea puteți introduce titlul sau cuvinte din titlul \ 54 cântării iar bot-ul va găsi piesa corespondentă.", 55 get_commands(songs_path).as_str() 56 )), 57 song_not_found: String::from("Niciun cântec găsit cu acest nume"), 58 report: ReportMsgs { 59 msg: String::from( 60 "Vă rugăm să trimiteți eroare pe care \ 61 ați găsit-o fie ca mesaj text sau vocal.", 62 ), 63 error_msg: String::from("Asta nu a mers. Încercați din nou sau /cancel"), 64 success_msg: String::from("A raportat corect corectia."), 65 cancel_msg: String::from("Raportarea a fost anulată."), 66 }, 67 }, 68 _ => Self { 69 lang, 70 start_msg: String::from(format!( 71 "Hello. This is a digital song book. :)\n\ 72 Commands:\n\ 73 /list - Lists all songs\n\ 74 {}\ 75 Otherwise simply type the title or parts of the title \ 76 of the song and you will receive the song.", 77 get_commands(songs_path).as_str() 78 )), 79 song_not_found: String::from("Didn't find any song with this title."), 80 report: ReportMsgs { 81 msg: String::from( 82 "Please send an error you found \ 83 either as text or voice message.", 84 ), 85 error_msg: String::from("That didn't work. Try again or /cancel."), 86 success_msg: String::from("Successfully reported your correction."), 87 cancel_msg: String::from("Reporting canceled."), 88 }, 89 }, 90 } 91 } 92 pub fn format(&self, name: &String) -> String { 93 let mut formatted_name = name.to_string(); 94 match self.lang.as_str() { 95 "de" => { 96 formatted_name = formatted_name.replace("Ö", "Oe"); 97 formatted_name = formatted_name.replace("ö", "oe"); 98 formatted_name = formatted_name.replace("Ü", "Ue"); 99 formatted_name = formatted_name.replace("ü", "ue"); 100 formatted_name = formatted_name.replace("Ä", "Ae"); 101 formatted_name = formatted_name.replace("ä", "ae"); 102 formatted_name = formatted_name.replace("ß", "ss"); 103 } 104 "md" => { 105 formatted_name = formatted_name.replace("ă", "a"); 106 formatted_name = formatted_name.replace("â", "a"); 107 formatted_name = formatted_name.replace("î", "i"); 108 formatted_name = formatted_name.replace("ș", "s"); 109 formatted_name = formatted_name.replace("ț", "t"); 110 } 111 _ => {} 112 } 113 formatted_name = formatted_name.replace("-", "_"); 114 formatted_name = formatted_name.replace(" ", "_"); 115 return formatted_name; 116 } 117 } 118 119 fn get_commands(songs_path: String) -> String { 120 let mut commands: String = String::new(); 121 for name in get_folder_names(&songs_path) { 122 commands.push_str(&("/".to_owned() + name.as_str() + "\n")); 123 } 124 return commands; 125 } 126 127 pub fn get_folder_names(songs_path: &String) -> Vec<String> { 128 let songs_dir = fs::read_dir(songs_path).unwrap(); 129 let mut folder_names: Vec<String> = vec![]; 130 let mut is_dir: bool; 131 let mut dir_entry; 132 for f in songs_dir { 133 dir_entry = f.expect("Error: f"); 134 is_dir = dir_entry.file_type().unwrap().is_dir(); 135 if is_dir { 136 let name: String = dir_entry.file_name().to_str().unwrap().to_string(); 137 folder_names.push(name) 138 } 139 } 140 return folder_names; 141 }