i18n.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (C) 2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package util
  15. import (
  16. "encoding/json"
  17. "errors"
  18. )
  19. // localization id for the Web frontend
  20. const (
  21. I18nLoginTitle = "title.login"
  22. I18nShareLoginTitle = "title.share_login"
  23. I18nFilesTitle = "title.files"
  24. I18nSharesTitle = "title.shares"
  25. I18nShareAddTitle = "title.add_share"
  26. I18nShareUpdateTitle = "title.update_share"
  27. I18nProfileTitle = "title.profile"
  28. I18nChangePwdTitle = "title.change_password"
  29. I18n2FATitle = "title.two_factor_auth"
  30. I18nEditFileTitle = "title.edit_file"
  31. I18nViewFileTitle = "title.view_file"
  32. I18nForgotPwdTitle = "title.recovery_password"
  33. I18nResetPwdTitle = "title.reset_password"
  34. I18nSharedFilesTitle = "title.shared_files"
  35. I18nShareUploadTitle = "title.upload_to_share"
  36. I18nShareDownloadTitle = "title.download_shared_file"
  37. I18nShareAccessErrorTitle = "title.share_access_error"
  38. I18nInvalidAuthReqTitle = "title.invalid_auth_request"
  39. I18nError403Title = "title.error403"
  40. I18nError400Title = "title.error400"
  41. I18nError404Title = "title.error404"
  42. I18nError416Title = "title.error416"
  43. I18nError429Title = "title.error429"
  44. I18nError500Title = "title.error500"
  45. I18nErrorPDFTitle = "title.errorPDF"
  46. I18nErrorEditorTitle = "title.error_editor"
  47. I18nInvalidAuth = "general.invalid_auth_request"
  48. I18nError429Message = "general.error429"
  49. I18nError400Message = "general.error400"
  50. I18nError403Message = "general.error403"
  51. I18nError404Message = "general.error404"
  52. I18nError416Message = "general.error416"
  53. I18nError500Message = "general.error500"
  54. I18nErrorPDFMessage = "general.errorPDF"
  55. I18nErrorInvalidToken = "general.invalid_token"
  56. I18nErrorInvalidForm = "general.invalid_form"
  57. I18nErrorInvalidCredentials = "general.invalid_credentials"
  58. I18nErrorInvalidCSRF = "general.invalid_csrf"
  59. I18nErrorFsGeneric = "fs.err_generic"
  60. I18nErrorDirListGeneric = "fs.dir_list.err_generic"
  61. I18nErrorDirList403 = "fs.dir_list.err_403"
  62. I18nErrorDirList429 = "fs.dir_list.err_429"
  63. I18nErrorDirListUser = "fs.dir_list.err_user"
  64. I18nErrorFsValidation = "fs.err_validation"
  65. I18nErrorChangePwdRequiredFields = "change_pwd.required_fields"
  66. I18nErrorChangePwdNoMatch = "change_pwd.no_match"
  67. I18nErrorChangePwdGeneric = "change_pwd.generic"
  68. I18nErrorChangePwdNoDifferent = "change_pwd.no_different"
  69. I18nErrorChangePwdCurrentNoMatch = "change_pwd.current_no_match"
  70. I18nErrorChangePwdRequired = "change_pwd.required"
  71. I18nErrorUsernameRequired = "general.username_required"
  72. I18nErrorGetUser = "general.err_user"
  73. I18nErrorPwdResetForbidded = "login.reset_pwd_forbidden"
  74. I18nErrorPwdResetNoEmail = "login.reset_pwd_no_email"
  75. I18nErrorPwdResetSendEmail = "login.reset_pwd_send_email_err"
  76. I18nErrorPwdResetGeneric = "login.reset_pwd_err_generic"
  77. I18nErrorProtocolForbidden = "general.err_protocol_forbidden"
  78. I18nErrorPwdLoginForbidden = "general.pwd_login_forbidden"
  79. I18nErrorIPForbidden = "general.ip_forbidden"
  80. I18nErrorConnectionForbidden = "general.connection_forbidden"
  81. I18nErrorReservedUsername = "user.username_reserved"
  82. I18nErrorInvalidEmail = "general.email_invalid"
  83. I18nErrorInvalidUser = "user.username_invalid"
  84. I18nErrorHomeRequired = "user.home_required"
  85. I18nErrorHomeInvalid = "user.home_invalid"
  86. I18nErrorPubKeyInvalid = "user.pub_key_invalid"
  87. I18nErrorPrimaryGroup = "user.err_primary_group"
  88. I18nErrorDuplicateGroup = "user.err_duplicate_group"
  89. I18nErrorNoPermission = "user.no_permissions"
  90. I18nErrorNoRootPermission = "user.no_root_permissions"
  91. I18nErrorGenericPermission = "user.err_permissions_generic"
  92. I18nError2FAInvalid = "user.2fa_invalid"
  93. I18nErrorRecoveryCodesInvalid = "user.recovery_codes_invalid"
  94. I18nErrorFolderNameRequired = "general.foldername_required"
  95. I18nErrorFolderMountPathRequired = "user.folder_path_required"
  96. I18nErrorDuplicatedFolders = "user.folder_duplicated"
  97. I18nErrorOverlappedFolders = "user.folder_overlapped"
  98. I18nErrorFolderQuotaSizeInvalid = "user.folder_quota_size_invalid"
  99. I18nErrorFolderQuotaFileInvalid = "user.folder_quota_file_invalid"
  100. I18nErrorFolderQuotaInvalid = "user.folder_quota_invalid"
  101. I18nErrorPasswordComplexity = "general.err_password_complexity"
  102. I18nErrorIPFiltersInvalid = "user.ip_filters_invalid"
  103. I18nErrorSourceBWLimitInvalid = "user.src_bw_limits_invalid"
  104. I18nErrorShareExpirationInvalid = "user.share_expiration_invalid"
  105. I18nErrorFilePatternPathInvalid = "user.file_pattern_path_invalid"
  106. I18nErrorFilePatternDuplicated = "user.file_pattern_duplicated"
  107. I18nErrorFilePatternInvalid = "user.file_pattern_invalid"
  108. I18nErrorDisableActive2FA = "user.disable_active_2fa"
  109. I18nErrorPwdChangeConflict = "user.pwd_change_conflict"
  110. I18nErrorLoginAfterReset = "login.reset_ok_login_error"
  111. I18nErrorShareScope = "share.scope_invalid"
  112. I18nErrorShareMaxTokens = "share.max_tokens_invalid"
  113. I18nErrorShareExpiration = "share.expiration_invalid"
  114. I18nErrorShareNoPwd = "share.err_no_password"
  115. I18nErrorShareExpirationOutOfRange = "share.expiration_out_of_range"
  116. I18nErrorShareGeneric = "share.generic"
  117. I18nErrorNameRequired = "general.name_required"
  118. I18nErrorSharePathRequired = "share.path_required"
  119. I18nErrorShareWriteScope = "share.path_write_scope"
  120. I18nErrorShareNestedPaths = "share.nested_paths"
  121. I18nErrorShareExpirationPast = "share.expiration_past"
  122. I18nErrorInvalidIPMask = "general.allowed_ip_mask_invalid"
  123. I18nErrorShareUsage = "share.usage_exceed"
  124. I18nErrorShareExpired = "share.expired"
  125. I18nErrorLoginFromIPDenied = "login.ip_not_allowed"
  126. I18nError2FARequired = "login.two_factor_required"
  127. I18nErrorNoOIDCFeature = "general.no_oidc_feature"
  128. I18nErrorNoPermissions = "general.no_permissions"
  129. I18nErrorShareBrowsePaths = "share.browsable_multiple_paths"
  130. I18nErrorShareBrowseNoDir = "share.browsable_non_dir"
  131. I18nErrorShareInvalidPath = "share.invalid_path"
  132. I18nErrorPathInvalid = "general.path_invalid"
  133. I18nErrorQuotaRead = "general.err_quota_read"
  134. I18nErrorEditDir = "general.error_edit_dir"
  135. I18nErrorEditSize = "general.error_edit_size"
  136. I18nProfileUpdated = "general.profile_updated"
  137. I18nShareLoginOK = "general.share_ok"
  138. I18n2FADisabled = "2fa.disabled"
  139. I18nOIDCTokenExpired = "oidc.token_expired"
  140. I18nOIDCTokenInvalidAdmin = "oidc.token_invalid_webadmin"
  141. I18nOIDCTokenInvalidUser = "oidc.token_invalid_webclient"
  142. I18nOIDCErrTokenExchange = "oidc.token_exchange_err"
  143. I18nOIDCTokenInvalid = "oidc.token_invalid"
  144. I18nOIDCTokenInvalidRoleAdmin = "oidc.role_admin_err"
  145. I18nOIDCTokenInvalidRoleUser = "oidc.role_user_err"
  146. I18nOIDCErrGetUser = "oidc.get_user_err"
  147. )
  148. // NewI18nError returns a I18nError wrappring the provided error
  149. func NewI18nError(err error, message string, options ...I18nErrorOption) *I18nError {
  150. var errI18n *I18nError
  151. if errors.As(err, &errI18n) {
  152. return errI18n
  153. }
  154. errI18n = &I18nError{
  155. err: err,
  156. Message: message,
  157. args: nil,
  158. }
  159. for _, opt := range options {
  160. opt(errI18n)
  161. }
  162. return errI18n
  163. }
  164. // I18nErrorOption defines a functional option type that allows to configure the I18nError.
  165. type I18nErrorOption func(*I18nError)
  166. // I18nErrorArgs is a functional option to set I18nError arguments.
  167. func I18nErrorArgs(args map[string]any) I18nErrorOption {
  168. return func(e *I18nError) {
  169. e.args = args
  170. }
  171. }
  172. // I18nError is an error wrapper that add a message to use for localization.
  173. type I18nError struct {
  174. err error
  175. Message string
  176. args map[string]any
  177. }
  178. // Error returns the wrapped error string.
  179. func (e *I18nError) Error() string {
  180. return e.err.Error()
  181. }
  182. // Unwrap returns the underlying error
  183. func (e *I18nError) Unwrap() error {
  184. return e.err
  185. }
  186. // Is reports if target matches
  187. func (e *I18nError) Is(target error) bool {
  188. if errors.Is(e.err, target) {
  189. return true
  190. }
  191. _, ok := target.(*I18nError)
  192. return ok
  193. }
  194. // HasArgs returns true if the error has i18n args.
  195. func (e *I18nError) HasArgs() bool {
  196. return len(e.args) > 0
  197. }
  198. // Args returns the provided args in JSON format
  199. func (e *I18nError) Args() string {
  200. if len(e.args) > 0 {
  201. data, err := json.Marshal(e.args)
  202. if err == nil {
  203. return string(data)
  204. }
  205. }
  206. return "{}"
  207. }