i18n.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "errors"
  17. )
  18. // localization id for the Web frontend
  19. const (
  20. I18nLoginTitle = "title.login"
  21. I18nShareLoginTitle = "title.share_login"
  22. I18nFilesTitle = "title.files"
  23. I18nSharesTitle = "title.shares"
  24. I18nShareAddTitle = "title.add_share"
  25. I18nShareUpdateTitle = "title.update_share"
  26. I18nProfileTitle = "title.profile"
  27. I18nChangePwdTitle = "title.change_password"
  28. I18n2FATitle = "title.two_factor_auth"
  29. I18nEditFileTitle = "title.edit_file"
  30. I18nViewFileTitle = "title.view_file"
  31. I18nForgotPwdTitle = "title.recovery_password"
  32. I18nResetPwdTitle = "title.reset_password"
  33. I18nSharedFilesTitle = "title.shared_files"
  34. I18nShareUploadTitle = "title.upload_to_share"
  35. I18nShareDownloadTitle = "title.download_shared_file"
  36. I18nShareAccessErrorTitle = "title.share_access_error"
  37. I18nInvalidAuthReqTitle = "title.invalid_auth_request"
  38. I18nError403Title = "title.error403"
  39. I18nError400Title = "title.error400"
  40. I18nError416Title = "title.error416"
  41. I18nError429Title = "title.error429"
  42. I18nError500Title = "title.error500"
  43. I18nErrorPDFTitle = "title.errorPDF"
  44. I18nErrorEditorTitle = "title.error_editor"
  45. I18nInvalidAuth = "general.invalid_auth_request"
  46. I18nError429Message = "general.error429"
  47. I18nError400Message = "general.error400"
  48. I18nError403Message = "general.error403"
  49. I18nError404Message = "general.error404"
  50. I18nError416Message = "general.error416"
  51. I18nError500Message = "general.error500"
  52. I18nErrorPDFMessage = "general.errorPDF"
  53. I18nErrorInvalidToken = "general.invalid_token"
  54. I18nErrorInvalidForm = "general.invalid_form"
  55. I18nErrorInvalidCredentials = "general.invalid_credentials"
  56. I18nErrorInvalidCSRF = "general.invalid_csrf"
  57. I18nErrorFsGeneric = "fs.err_generic"
  58. I18nErrorDirListGeneric = "fs.dir_list.err_generic"
  59. I18nErrorDirList403 = "fs.dir_list.err_403"
  60. I18nErrorDirList429 = "fs.dir_list.err_429"
  61. I18nErrorDirListUser = "fs.dir_list.err_user"
  62. I18nErrorFsValidation = "fs.err_validation"
  63. I18nErrorChangePwdRequiredFields = "change_pwd.required_fields"
  64. I18nErrorChangePwdNoMatch = "change_pwd.no_match"
  65. I18nErrorChangePwdGeneric = "change_pwd.generic"
  66. I18nErrorChangePwdNoDifferent = "change_pwd.no_different"
  67. I18nErrorChangePwdCurrentNoMatch = "change_pwd.current_no_match"
  68. I18nErrorChangePwdRequired = "change_pwd.required"
  69. I18nErrorUsernameRequired = "general.username_required"
  70. I18nErrorGetUser = "general.err_user"
  71. I18nErrorPwdResetForbidded = "login.reset_pwd_forbidden"
  72. I18nErrorPwdResetNoEmail = "login.reset_pwd_no_email"
  73. I18nErrorPwdResetSendEmail = "login.reset_pwd_send_email_err"
  74. I18nErrorPwdResetGeneric = "login.reset_pwd_err_generic"
  75. I18nErrorProtocolForbidden = "general.err_protocol_forbidden"
  76. I18nErrorPwdLoginForbidden = "general.pwd_login_forbidden"
  77. I18nErrorIPForbidden = "general.ip_forbidden"
  78. I18nErrorConnectionForbidden = "general.connection_forbidden"
  79. I18nErrorReservedUsername = "user.username_reserved"
  80. I18nErrorInvalidEmail = "general.email_invalid"
  81. I18nErrorInvalidUser = "user.username_invalid"
  82. I18nErrorHomeRequired = "user.home_required"
  83. I18nErrorHomeInvalid = "user.home_invalid"
  84. I18nErrorPubKeyInvalid = "user.pub_key_invalid"
  85. I18nErrorPrimaryGroup = "user.err_primary_group"
  86. I18nErrorDuplicateGroup = "user.err_duplicate_group"
  87. I18nErrorNoPermission = "user.no_permissions"
  88. I18nErrorNoRootPermission = "user.no_root_permissions"
  89. I18nErrorGenericPermission = "user.err_permissions_generic"
  90. I18nError2FAInvalid = "user.2fa_invalid"
  91. I18nErrorRecoveryCodesInvalid = "user.recovery_codes_invalid"
  92. I18nErrorFolderNameRequired = "general.foldername_required"
  93. I18nErrorFolderMountPathRequired = "user.folder_path_required"
  94. I18nErrorDuplicatedFolders = "user.folder_duplicated"
  95. I18nErrorOverlappedFolders = "user.folder_overlapped"
  96. I18nErrorFolderQuotaSizeInvalid = "user.folder_quota_size_invalid"
  97. I18nErrorFolderQuotaFileInvalid = "user.folder_quota_file_invalid"
  98. I18nErrorFolderQuotaInvalid = "user.folder_quota_invalid"
  99. I18nErrorPasswordComplexity = "general.err_password_complexity"
  100. I18nErrorIPFiltersInvalid = "user.ip_filters_invalid"
  101. I18nErrorSourceBWLimitInvalid = "user.src_bw_limits_invalid"
  102. I18nErrorShareExpirationInvalid = "user.share_expiration_invalid"
  103. I18nErrorFilePatternPathInvalid = "user.file_pattern_path_invalid"
  104. I18nErrorFilePatternDuplicated = "user.file_pattern_duplicated"
  105. I18nErrorFilePatternInvalid = "user.file_pattern_invalid"
  106. I18nErrorDisableActive2FA = "user.disable_active_2fa"
  107. I18nErrorPwdChangeConflict = "user.pwd_change_conflict"
  108. I18nErrorLoginAfterReset = "login.reset_ok_login_error"
  109. I18nErrorShareScope = "share.scope_invalid"
  110. I18nErrorShareMaxTokens = "share.max_tokens_invalid"
  111. I18nErrorShareExpiration = "share.expiration_invalid"
  112. I18nErrorShareNoPwd = "share.err_no_password"
  113. I18nErrorShareExpirationOutOfRange = "share.expiration_out_of_range"
  114. I18nErrorShareGeneric = "share.generic"
  115. I18nErrorNameRequired = "general.name_required"
  116. I18nErrorSharePathRequired = "share.path_required"
  117. I18nErrorShareWriteScope = "share.path_write_scope"
  118. I18nErrorShareNestedPaths = "share.nested_paths"
  119. I18nErrorShareExpirationPast = "share.expiration_past"
  120. I18nErrorInvalidIPMask = "general.allowed_ip_mask_invalid"
  121. I18nErrorShareUsage = "share.usage_exceed"
  122. I18nErrorShareExpired = "share.expired"
  123. I18nErrorLoginFromIPDenied = "login.ip_not_allowed"
  124. I18nError2FARequired = "login.two_factor_required"
  125. I18nErrorNoOIDCFeature = "general.no_oidc_feature"
  126. I18nErrorNoPermissions = "general.no_permissions"
  127. I18nErrorShareBrowsePaths = "share.browsable_multiple_paths"
  128. I18nErrorShareBrowseNoDir = "share.browsable_non_dir"
  129. I18nErrorPathInvalid = "general.path_invalid"
  130. I18nErrorQuotaRead = "general.err_quota_read"
  131. I18nErrorEditDir = "general.error_edit_dir"
  132. I18nErrorEditSize = "general.error_edit_size"
  133. I18nProfileUpdated = "general.profile_updated"
  134. I18nShareLoginOK = "general.share_ok"
  135. I18n2FADisabled = "2fa.disabled"
  136. )
  137. // NewI18nError returns a I18nError wrappring the provided error
  138. func NewI18nError(err error, message string) *I18nError {
  139. var errI18n *I18nError
  140. if errors.As(err, &errI18n) {
  141. return errI18n
  142. }
  143. return &I18nError{
  144. err: err,
  145. I18nMessage: message,
  146. }
  147. }
  148. // I18nError is an error wrapper that add a message to use for localization.
  149. type I18nError struct {
  150. err error
  151. I18nMessage string
  152. }
  153. // Error returns the wrapped error string.
  154. func (e *I18nError) Error() string {
  155. return e.err.Error()
  156. }
  157. // Is reports if target matches
  158. func (e *I18nError) Is(target error) bool {
  159. if errors.Is(e.err, target) {
  160. return true
  161. }
  162. _, ok := target.(*I18nError)
  163. return ok
  164. }