i18n.go 10.0 KB

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