#pragma once #include #include #include "common/protocol_def.h" namespace im { namespace i18n { // 语言:由 Client 连接后 SetConfig 下发,存于 Session;客户端写死上报值与兜底文案 enum class Lang { Zh = 0, En = 1, }; // 回包 msg / 客户端兜底文案的键 enum class Msg { Ok = 0, Kicked, InvalidJson, NotLogin, UidPasswordRequired, UidOrPasswordIncorrect, AccountDisabled, SystemAccount, IncompleteRegisterInfo, NicknameTooLong, BadAccountFormat, WeakPassword, InvalidPhone, AccountExists, PhoneExists, BadSms, RegisterFailed, SceneMustBeRegisterOrReset, PhoneNotRegistered, SmsTooFrequent, SmsSendFailed, IncompleteResetInfo, UserNotFound, PhoneMismatch, AccountNotAllowed, OldPasswordRequired, OldPasswordIncorrect, ToUidContentRequired, ContentTooLong, PeerUidRequired, UidRequired, CannotAddYourself, AlreadyFriends, FriendRequestSent, AddFriendNotAllowed, FriendRequestPending, FriendRequestMessageTooLong, SearchKeywordRequired, StickerNotFound, DbError, BadParam, RequestFailed, NotFriends, RemarkTooLong, FileTooLarge, FileNotFound, AvatarInvalid, SignatureTooLong, CannotOperateSystemFriend, FavoriteNotFound, FavoriteContentTooLong, GroupNotFound, GroupNameRequired, GroupNameTooLong, NotGroupMember, GroupNoPermission, GroupFull, JoinNotAllowed, AlreadyInGroup, OwnerCannotLeave, GroupNeedMembers, GroupRequestSent, GroupRequestPending, GroupAdminFull, CannotKickOwner, GroupIdRequired, _Count }; namespace detail { struct Entry { const char* zh; const char* en; }; inline Lang& current() { static Lang g = Lang::Zh; return g; } inline const Entry& table(Msg id) { static const Entry k[static_cast(Msg::_Count)] = { {"成功", "OK"}, {"账号已在其他设备登录", "Signed in on another device"}, {"请求数据无效", "Invalid request data"}, {"未登录", "Not logged in"}, {"请输入账号和密码", "Account and password required"}, {"账号或密码错误", "Incorrect account or password"}, {"账号已禁用", "Account disabled"}, {"系统账号不可登录", "System account"}, {"注册信息不完整", "Incomplete register info"}, {"昵称过长", "Nickname too long"}, {"账号须为4-16位字母或数字", "Account must be 4-16 letters or digits"}, {"密码须为6-20个字符", "Password must be 6-20 characters"}, {"手机号无效", "Invalid phone number"}, {"账号已存在", "Account already exists"}, {"手机号已注册", "Phone already registered"}, {"验证码错误或已过期", "Invalid or expired SMS code"}, {"注册失败", "Register failed"}, {"场景须为 register 或 reset", "Scene must be register or reset"}, {"手机号未注册", "Phone not registered"}, {"验证码发送过于频繁", "SMS too frequent"}, {"短信发送失败", "SMS send failed"}, {"重置信息不完整", "Incomplete reset info"}, {"用户不存在", "User not found"}, {"手机号与账号不匹配", "Phone mismatch"}, {"账号不允许此操作", "Account not allowed"}, {"请输入原密码和新密码", "Old password and password required"}, {"原密码错误", "Old password incorrect"}, {"请指定接收方和消息内容", "to_uid and content required"}, {"消息内容过长", "Content too long"}, {"请指定会话对方", "peer_uid required"}, {"请指定用户", "uid required"}, {"不能添加自己为好友", "Cannot add yourself"}, {"已经是好友", "Already friends"}, {"好友申请已发送,等待对方同意", "Friend request sent, waiting for approval"}, {"对方不允许添加好友", "This user does not accept friend requests"}, {"已发送过申请,请等待对方处理", "Request already sent"}, {"验证信息过长", "Verification message too long"}, {"请输入账号或手机号", "Account or phone required"}, {"表情不存在", "Sticker not found"}, {"服务暂时不可用", "Service temporarily unavailable"}, {"参数错误", "Invalid parameter"}, {"请求失败", "Request failed"}, {"还不是好友", "Not friends"}, {"备注过长", "Remark too long"}, {"文件过大", "File too large"}, {"文件不存在", "File not found"}, {"头像无效", "Invalid avatar"}, {"签名过长", "Signature too long"}, {"系统账号不支持此操作", "System account does not support this action"}, {"收藏不存在", "Favorite not found"}, {"收藏内容过长", "Favorite content too long"}, {"群聊不存在", "Group not found"}, {"请输入群名称", "Group name required"}, {"群名称过长", "Group name too long"}, {"你不是群成员", "Not a group member"}, {"没有权限", "No permission"}, {"群人数已满", "Group is full"}, {"该群不允许加入", "This group does not allow joining"}, {"已在群中", "Already in the group"}, {"群主不能退群,请先转让或解散", "Owner cannot leave; transfer or dismiss first"}, {"请至少选择一名好友", "Select at least one friend"}, {"入群申请已发送", "Join request sent"}, {"已发送过申请,请等待处理", "Request already sent"}, {"管理员人数已满", "Too many admins"}, {"不能移除群主", "Cannot remove the owner"}, {"请指定群聊", "group_id required"}, }; const int i = static_cast(id); if (i < 0 || i >= static_cast(Msg::_Count)) { return k[static_cast(Msg::RequestFailed)]; } return k[i]; } } // namespace detail inline Lang lang() { return detail::current(); } inline void set_lang(Lang l) { detail::current() = l; } inline Lang parse_lang(const char* s) { if (s == nullptr || s[0] == '\0') { return Lang::Zh; } if ((s[0] == 'e' || s[0] == 'E') && (s[1] == 'n' || s[1] == 'N')) { return Lang::En; } return Lang::Zh; } inline Lang parse_lang(const std::string& s) { return parse_lang(s.c_str()); } inline const char* text(Msg id, Lang l) { const detail::Entry& e = detail::table(id); return l == Lang::En ? e.en : e.zh; } inline const char* text(Msg id) { return text(id, lang()); } // 按错误码兜底(客户端在服务端未带详细 msg 时使用) inline Msg msg_of_err(int code) { switch (code) { case Err::Ok: return Msg::Ok; case Err::BadParam: return Msg::BadParam; case Err::NotLogin: return Msg::NotLogin; case Err::AuthFail: return Msg::UidOrPasswordIncorrect; case Err::NotFound: return Msg::UserNotFound; case Err::Exists: return Msg::AccountExists; case Err::PhoneExists: return Msg::PhoneExists; case Err::BadSms: return Msg::BadSms; case Err::WeakPass: return Msg::WeakPassword; case Err::Forbidden: return Msg::AccountNotAllowed; case Err::DbError: return Msg::DbError; case Err::BadAccount: return Msg::BadAccountFormat; case Err::RateLimit: return Msg::SmsTooFrequent; case Err::Disabled: return Msg::AccountDisabled; default: return Msg::RequestFailed; } } inline const char* err_text(int code) { return text(msg_of_err(code)); } } // namespace i18n } // namespace im