You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android中实现Firebase Authentication注册错误信息翻译?

Android (Java) 实现Firebase注册错误信息自定义本地化

你在iOS端通过判断AuthErrorCode枚举来定制错误提示的思路,在Android的Firebase Auth里完全可以照搬——我们只需要捕获FirebaseAuthException,提取具体错误码,再根据不同错误类型返回对应的翻译文本就行。

直接给你适配好的代码,替换掉你现有addOnFailureListener里的逻辑:

FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(task -> {
            // 处理注册成功的逻辑
        })
        .addOnFailureListener(exception -> {
            if (exception instanceof FirebaseAuthException) {
                FirebaseAuthException authEx = (FirebaseAuthException) exception;
                String errorCode = authEx.getErrorCode();

                String alertTitle = "Mohon Maaf";
                String alertMsg = "";

                // 匹配对应错误码,返回印尼语提示
                switch (errorCode) {
                    case "ERROR_NETWORK_REQUEST_FAILED":
                        alertMsg = "Koneksi Internet bermasalah";
                        break;
                    case "ERROR_EMAIL_ALREADY_IN_USE":
                        alertMsg = "Email yang anda masukan sudah pernah digunakan, silahkan gunakan email yang lain";
                        break;
                    case "ERROR_WEAK_PASSWORD":
                        alertMsg = "Password minimal harus memiliki 6 huruf";
                        break;
                    case "ERROR_INVALID_EMAIL":
                        alertMsg = "Format email yang anda masukan tidak valid, mohon diperiksa kembali.";
                        break;
                    default:
                        // 未匹配到的错误,用默认英文提示
                        alertTitle = "Sorry";
                        alertMsg = exception.getLocalizedMessage();
                        break;
                }

                // 调用你自己的弹窗展示方法,和iOS的showAlert逻辑一致
                showAlert(alertTitle, alertMsg, "Kembali");
            }
        });

几个关键细节:

  • 错误码对应:Android的Firebase Auth错误码是字符串常量,我已经帮你和iOS端的枚举做了对齐,直接使用即可;
  • 类型强转:必须把捕获到的异常强转为FirebaseAuthException,才能调用getErrorCode()获取具体的错误标识;
  • 弹窗实现:你需要自己实现showAlert方法,比如用Android原生的AlertDialog来弹出提示,逻辑和iOS端的showAlert保持统一就好。

这样就能让Android端的错误提示和iOS端完全同步,给用户一致的多语言体验啦。

内容的提问来源于stack exchange,提问作者Agung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:24:38