本地化 Firebase error.localizedDescription 的最佳方法是什么?

时间:2021-04-23 18:38:24

标签: firebase error-handling swiftui localization

我能够定位 Firebase 错误,但我很确定我没有使用最佳方式。

我在 Localizable.strings 文件中使用 AuthErrorCode 扩展和 NSLocalizedString 和字符串:

extension AuthErrorCode {
    var errorMessage: String {
        switch self {
           case .missingEmail:
                return NSLocalizedString("FBEmailMissing", comment: "")
...

查看:

...

@State var AuthErrorCodeString: AuthErrorCode?

...
                        FirebaseSession_VM.resetPassword(email : email) { (result) in
                            switch result {
                                case .failure(let error):
                                    
                                    // on va trapper les erreurs ici!!!!
                                    // errString = error.localizedDescription
                                    AuthErrorCodeString = AuthErrorCode(rawValue: error._code)
                                case .success(_):
                                    break
                            }
...

                .alert(isPresented: $showAlert) {
                    Alert(title: Text("Password Reset"),
                          message: Text(AuthErrorCodeString?.errorMessage ?? "Success. Reset email sent successfully. check your email"), dismissButton: .default(Text("OK")) {
                            if AuthErrorCodeString?.errorMessage == nil {
                                self.presentationMode.wrappedValue.dismiss()
                            }
                          })
                }

Localizable.strings(英文)


"FBEmailMissing" = "Email is missing!!!";

Localizable.strings(法语)

"FBEmailMissing" = "Courriel manquant!!!";

我的代码正在运行,但我想使用最佳方式。你有什么建议?

谢谢

1 个答案:

答案 0 :(得分:0)

我不是专家,但我认为没有更好的方法