i18n.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma once
  2. #include <cstring>
  3. #include <string>
  4. #include "common/protocol_def.h"
  5. namespace im {
  6. namespace i18n {
  7. // 语言:由 Client 连接后 SetConfig 下发,存于 Session;客户端写死上报值与兜底文案
  8. enum class Lang {
  9. Zh = 0,
  10. En = 1,
  11. };
  12. // 回包 msg / 客户端兜底文案的键
  13. enum class Msg {
  14. Ok = 0,
  15. Kicked,
  16. InvalidJson,
  17. NotLogin,
  18. UidPasswordRequired,
  19. UidOrPasswordIncorrect,
  20. AccountDisabled,
  21. SystemAccount,
  22. IncompleteRegisterInfo,
  23. NicknameTooLong,
  24. BadAccountFormat,
  25. WeakPassword,
  26. InvalidPhone,
  27. AccountExists,
  28. PhoneExists,
  29. BadSms,
  30. RegisterFailed,
  31. SceneMustBeRegisterOrReset,
  32. PhoneNotRegistered,
  33. SmsTooFrequent,
  34. SmsSendFailed,
  35. IncompleteResetInfo,
  36. UserNotFound,
  37. PhoneMismatch,
  38. AccountNotAllowed,
  39. OldPasswordRequired,
  40. OldPasswordIncorrect,
  41. ToUidContentRequired,
  42. ContentTooLong,
  43. PeerUidRequired,
  44. UidRequired,
  45. CannotAddYourself,
  46. AlreadyFriends,
  47. FriendRequestSent,
  48. AddFriendNotAllowed,
  49. FriendRequestPending,
  50. FriendRequestMessageTooLong,
  51. SearchKeywordRequired,
  52. StickerNotFound,
  53. DbError,
  54. BadParam,
  55. RequestFailed,
  56. NotFriends,
  57. RemarkTooLong,
  58. FileTooLarge,
  59. FileNotFound,
  60. AvatarInvalid,
  61. SignatureTooLong,
  62. CannotOperateSystemFriend,
  63. FavoriteNotFound,
  64. FavoriteContentTooLong,
  65. GroupNotFound,
  66. GroupNameRequired,
  67. GroupNameTooLong,
  68. NotGroupMember,
  69. GroupNoPermission,
  70. GroupFull,
  71. JoinNotAllowed,
  72. AlreadyInGroup,
  73. OwnerCannotLeave,
  74. GroupNeedMembers,
  75. GroupRequestSent,
  76. GroupRequestPending,
  77. GroupAdminFull,
  78. CannotKickOwner,
  79. GroupIdRequired,
  80. _Count
  81. };
  82. namespace detail {
  83. struct Entry {
  84. const char* zh;
  85. const char* en;
  86. };
  87. inline Lang& current()
  88. {
  89. static Lang g = Lang::Zh;
  90. return g;
  91. }
  92. inline const Entry& table(Msg id)
  93. {
  94. static const Entry k[static_cast<int>(Msg::_Count)] = {
  95. {"成功", "OK"},
  96. {"账号已在其他设备登录", "Signed in on another device"},
  97. {"请求数据无效", "Invalid request data"},
  98. {"未登录", "Not logged in"},
  99. {"请输入账号和密码", "Account and password required"},
  100. {"账号或密码错误", "Incorrect account or password"},
  101. {"账号已禁用", "Account disabled"},
  102. {"系统账号不可登录", "System account"},
  103. {"注册信息不完整", "Incomplete register info"},
  104. {"昵称过长", "Nickname too long"},
  105. {"账号须为4-16位字母或数字", "Account must be 4-16 letters or digits"},
  106. {"密码须为6-20个字符", "Password must be 6-20 characters"},
  107. {"手机号无效", "Invalid phone number"},
  108. {"账号已存在", "Account already exists"},
  109. {"手机号已注册", "Phone already registered"},
  110. {"验证码错误或已过期", "Invalid or expired SMS code"},
  111. {"注册失败", "Register failed"},
  112. {"场景须为 register 或 reset", "Scene must be register or reset"},
  113. {"手机号未注册", "Phone not registered"},
  114. {"验证码发送过于频繁", "SMS too frequent"},
  115. {"短信发送失败", "SMS send failed"},
  116. {"重置信息不完整", "Incomplete reset info"},
  117. {"用户不存在", "User not found"},
  118. {"手机号与账号不匹配", "Phone mismatch"},
  119. {"账号不允许此操作", "Account not allowed"},
  120. {"请输入原密码和新密码", "Old password and password required"},
  121. {"原密码错误", "Old password incorrect"},
  122. {"请指定接收方和消息内容", "to_uid and content required"},
  123. {"消息内容过长", "Content too long"},
  124. {"请指定会话对方", "peer_uid required"},
  125. {"请指定用户", "uid required"},
  126. {"不能添加自己为好友", "Cannot add yourself"},
  127. {"已经是好友", "Already friends"},
  128. {"好友申请已发送,等待对方同意", "Friend request sent, waiting for approval"},
  129. {"对方不允许添加好友", "This user does not accept friend requests"},
  130. {"已发送过申请,请等待对方处理", "Request already sent"},
  131. {"验证信息过长", "Verification message too long"},
  132. {"请输入账号或手机号", "Account or phone required"},
  133. {"表情不存在", "Sticker not found"},
  134. {"服务暂时不可用", "Service temporarily unavailable"},
  135. {"参数错误", "Invalid parameter"},
  136. {"请求失败", "Request failed"},
  137. {"还不是好友", "Not friends"},
  138. {"备注过长", "Remark too long"},
  139. {"文件过大", "File too large"},
  140. {"文件不存在", "File not found"},
  141. {"头像无效", "Invalid avatar"},
  142. {"签名过长", "Signature too long"},
  143. {"系统账号不支持此操作", "System account does not support this action"},
  144. {"收藏不存在", "Favorite not found"},
  145. {"收藏内容过长", "Favorite content too long"},
  146. {"群聊不存在", "Group not found"},
  147. {"请输入群名称", "Group name required"},
  148. {"群名称过长", "Group name too long"},
  149. {"你不是群成员", "Not a group member"},
  150. {"没有权限", "No permission"},
  151. {"群人数已满", "Group is full"},
  152. {"该群不允许加入", "This group does not allow joining"},
  153. {"已在群中", "Already in the group"},
  154. {"群主不能退群,请先转让或解散", "Owner cannot leave; transfer or dismiss first"},
  155. {"请至少选择一名好友", "Select at least one friend"},
  156. {"入群申请已发送", "Join request sent"},
  157. {"已发送过申请,请等待处理", "Request already sent"},
  158. {"管理员人数已满", "Too many admins"},
  159. {"不能移除群主", "Cannot remove the owner"},
  160. {"请指定群聊", "group_id required"},
  161. };
  162. const int i = static_cast<int>(id);
  163. if (i < 0 || i >= static_cast<int>(Msg::_Count)) {
  164. return k[static_cast<int>(Msg::RequestFailed)];
  165. }
  166. return k[i];
  167. }
  168. } // namespace detail
  169. inline Lang lang()
  170. {
  171. return detail::current();
  172. }
  173. inline void set_lang(Lang l)
  174. {
  175. detail::current() = l;
  176. }
  177. inline Lang parse_lang(const char* s)
  178. {
  179. if (s == nullptr || s[0] == '\0') {
  180. return Lang::Zh;
  181. }
  182. if ((s[0] == 'e' || s[0] == 'E') && (s[1] == 'n' || s[1] == 'N')) {
  183. return Lang::En;
  184. }
  185. return Lang::Zh;
  186. }
  187. inline Lang parse_lang(const std::string& s)
  188. {
  189. return parse_lang(s.c_str());
  190. }
  191. inline const char* text(Msg id, Lang l)
  192. {
  193. const detail::Entry& e = detail::table(id);
  194. return l == Lang::En ? e.en : e.zh;
  195. }
  196. inline const char* text(Msg id)
  197. {
  198. return text(id, lang());
  199. }
  200. // 按错误码兜底(客户端在服务端未带详细 msg 时使用)
  201. inline Msg msg_of_err(int code)
  202. {
  203. switch (code) {
  204. case Err::Ok:
  205. return Msg::Ok;
  206. case Err::BadParam:
  207. return Msg::BadParam;
  208. case Err::NotLogin:
  209. return Msg::NotLogin;
  210. case Err::AuthFail:
  211. return Msg::UidOrPasswordIncorrect;
  212. case Err::NotFound:
  213. return Msg::UserNotFound;
  214. case Err::Exists:
  215. return Msg::AccountExists;
  216. case Err::PhoneExists:
  217. return Msg::PhoneExists;
  218. case Err::BadSms:
  219. return Msg::BadSms;
  220. case Err::WeakPass:
  221. return Msg::WeakPassword;
  222. case Err::Forbidden:
  223. return Msg::AccountNotAllowed;
  224. case Err::DbError:
  225. return Msg::DbError;
  226. case Err::BadAccount:
  227. return Msg::BadAccountFormat;
  228. case Err::RateLimit:
  229. return Msg::SmsTooFrequent;
  230. case Err::Disabled:
  231. return Msg::AccountDisabled;
  232. default:
  233. return Msg::RequestFailed;
  234. }
  235. }
  236. inline const char* err_text(int code)
  237. {
  238. return text(msg_of_err(code));
  239. }
  240. } // namespace i18n
  241. } // namespace im