forked from MightyPork/group-actor
				
			
							parent
							
								
									881411ebd3
								
							
						
					
					
						commit
						bd47a004bf
					
				| @ -0,0 +1,18 @@ | ||||
| { | ||||
|   "welcome_public": "@{user} Welcome to the group! The group user will now follow you back to complete the sign-up.\nTo share a post, @ the group user or use a group hashtag.\n\nUse /help for more info.", | ||||
|   "welcome_member_only": "@{user} Welcome to the group! This group has posting restricted to members.\nIf you'd like to join, please ask one of the group admins:\n{admins}", | ||||
|   "welcome_join_cmd": "Welcome to the group! The group user will now follow you to complete the sign-up. Make sure you follow back to receive shared posts!\n\nUse /help for more info.", | ||||
|   "welcome_closed": "Sorry, this group is closed to new sign-ups.\nPlease ask one of the group admins to add you:", | ||||
|   "user_list_member": "- {user}", | ||||
|   "user_list_admin": "- {user} [admin]", | ||||
|   "help_admin_commands": "\n**Admin commands:**\n`/ping` - check the group works\n`/add user` - add a member (user@domain)\n`/remove user` - remove a member\n`/add #hashtag` - add a group hashtag\n`/remove #hashtag` - remove a group hashtag\n`/undo` - un-boost a replied-to post, delete an announcement\n`/ban x` - ban a user or server\n`/unban x` - lift a ban\n`/admin user` - grant admin rights\n`/deadmin user` - revoke admin rights\n`/closegroup` - make member-only\n`/opengroup` - make public-access\n`/announce x` - make a public announcement", | ||||
|   "cmd_leave_resp": "You're no longer a group member. Unfollow the group user to stop receiving group messages.", | ||||
|   "member_list_heading": "Group members:", | ||||
|   "admin_list_heading": "Group admins:", | ||||
|   "tag_list_heading": "Group tags:", | ||||
|   "tag_list_entry": "- {tag}", | ||||
|   "cmd_close_resp": "Group changed to member-only", | ||||
|   "cmd_close_resp_noaction": "No action, group is member-only already", | ||||
|   "cmd_open_resp": "Group changed to open-access", | ||||
|   "cmd_open_resp_noaction": "No action, group is open-access already", | ||||
| } | ||||
| @ -0,0 +1,81 @@ | ||||
| //! magic for custom translations and strings
 | ||||
| 
 | ||||
| use std::collections::HashMap; | ||||
| use once_cell::sync::Lazy; | ||||
| 
 | ||||
| #[derive(Debug,Clone,Serialize,Deserialize,Default)] | ||||
| pub struct TranslationTable { | ||||
|     #[serde(flatten)] | ||||
|     entries: Option<HashMap<String, String>>, | ||||
| } | ||||
| 
 | ||||
| pub const EMPTY_TRANSLATION_TABLE : TranslationTable = TranslationTable { | ||||
|     entries: None, | ||||
| }; | ||||
| 
 | ||||
| impl TranslationTable { | ||||
|     pub fn new() -> Self { | ||||
|         Self::default() | ||||
|     } | ||||
| 
 | ||||
|     pub fn add_translation(&mut self, key : impl ToString, subs : impl ToString) { | ||||
|         if self.entries.is_none() { | ||||
|             self.entries = Some(Default::default()); | ||||
|         } | ||||
|         self.entries.as_mut().unwrap().insert(key.to_string(), subs.to_string()); | ||||
|     } | ||||
| 
 | ||||
|     pub fn subs(&self, key : &str, substitutions: &[&str]) -> String { | ||||
|         if let Some(ee) = &self.entries { | ||||
|             match ee.get(key) { | ||||
|                 Some(s) => { | ||||
|                     // TODO optimize
 | ||||
|                     let mut s = s.clone(); | ||||
|                     for pair in substitutions.chunks(2) { | ||||
|                         if pair.len() != 2 { | ||||
|                             continue; | ||||
|                         } | ||||
|                         s = s.replace(&format!("{{{}}}", pair[0]), pair[1]); | ||||
|                     } | ||||
|                     s | ||||
|                 } | ||||
|                 None => key.to_owned() | ||||
|             } | ||||
|         } else { | ||||
|             key.to_owned() | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use crate::tr::TranslationTable; | ||||
| 
 | ||||
|     #[test] | ||||
|     fn deser_tr_table() { | ||||
|         let tr : TranslationTable = serde_json::from_str(r#"{"foo":"bar"}"#).unwrap(); | ||||
|         assert_eq!("bar", tr.subs("foo", &[])); | ||||
|         assert_eq!("xxx", tr.subs("xxx", &[])); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     #[test] | ||||
|     fn subs() { | ||||
|         let mut tr = TranslationTable::new(); | ||||
|         tr.add_translation("hello_user", "Hello, {user}!"); | ||||
|         assert_eq!("Hello, James!", tr.subs("hello_user", &["user", "James"])); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[macro_export] | ||||
| macro_rules! tr { | ||||
|     ($tr_haver:expr, $key:literal) => { | ||||
|         $tr_haver.tr().subs($key, &[]) | ||||
|     }; | ||||
| 
 | ||||
|     ($tr_haver:expr, $key:literal, $($k:tt=$value:expr),*) => { | ||||
|         $tr_haver.tr().subs($key, &[ | ||||
|             $(stringify!($k), $value),* | ||||
|         ]) | ||||
|     }; | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue