error.localizedDescription always returns English

江枫思渺然 提交于 2020-07-22 09:46:28

问题


In Swift/XCode I always get the English error messages as an output and not the translated local message of my device (Simulator or real device doesn't matter). I added the localization countries in the info tap of my project, but do I have to do something else, like translating an English error file manually to e.g. German?

I know there are similar old questions on SO, and their answers all sound very simple but don't work.

Example code:

ContentView.swift

@State var errorText: String = ""

var body: some View {
    VStack {
       ...
       logIn()
       Text(errorText)
    }
}

.....

    func logIn() {

        session.logIn(email: email, password: password) { (user, error) in

            if let error = error
                {
                self.errorText = error.localizedDescription
                    self.shouldAnimate = false

                return
                }
        ....

Edit:

I wasn't sure what I should enter here when adding German as a language. I use SwiftUI and not Storyboard if this fact matters:

What does work for example is the translation of the Edit Button in the Status Bar. But the error messages at the same time are always English.

.navigationBarItems(trailing: EditButton())

回答1:


The solution was to build an extension for AuthErrorCode. I came on this because of this answer: https://stackoverflow.com/a/49916157/12637422

The Localizable.strings only worked on Text("...") for me.

A solution for 2 languages could look like this (Polish and German example):

import Foundation
import Firebase

extension AuthErrorCode {
    var description: String? {
        switch self {
        case .emailAlreadyInUse:
            if Locale.current.languageCode == "pl" {
                return "Adres e-mail jest już w użyciu." } else {
                return "Die E-Mailadresse wird bereits verwendet."
            }
        case .userDisabled:
            if Locale.current.languageCode == "pl" {
                return "Użytkownik jest obecnie dezaktywowany. Skontaktuj się z poliapp@polipol.de." } else {
                return "Der Benutzer ist zur Zeit deaktiviert. Bitte an poliapp@polipol.de wenden."
            }
        case .operationNotAllowed:
            if Locale.current.languageCode == "pl" {
                return "Operacja niezatwierdzona." } else {
                return "Operation nicht genehmigt."
            }
        case .invalidEmail:
            if Locale.current.languageCode == "pl" {
                return "Format adresu e-mail jest nieprawidłowy." } else {
                return "Das Format der E-Mailadresse ist ungültig."
            }
        case .wrongPassword:
            if Locale.current.languageCode == "pl" {
                return "Hasło jest nieprawidłowe." } else {
                return "Das Passwort ist nicht korrekt."
            }
        case .userNotFound:
            if Locale.current.languageCode == "pl" {
                return "Nie znaleziono odpowiedniego konta użytkownika." } else {
                return "Kein entsprechendes Benutzerkonto gefunden."
            }
        case .networkError:
            if Locale.current.languageCode == "pl" {
                return "Wystąpił błąd sieci. Proszę spróbuj ponownie." } else {
                return "Es ist ein Netzwerkfehler aufgetreten. Bitte erneut versuchen."
            }
        case .weakPassword:
            if Locale.current.languageCode == "pl" {
                return "Hasło jest za słabe." } else {
                return "Das Passwort ist zu schwach."
            }
        case .missingEmail:
            if Locale.current.languageCode == "pl" {
                return "Proszę podać adres e-mail." } else {
                return "Bitte eine E-Mailadresse angeben."
            }
        case .internalError:
            if Locale.current.languageCode == "pl" {
                return "Błąd wewnętrzny Proszę spróbuj ponownie." } else {
                return "Interner Fehler. Bitte erneut versuchen."
            }
        case .invalidCustomToken:
            if Locale.current.languageCode == "pl" {
                return "Token jest nieprawidłowy." } else {
                return "Das Token ist ungültig."
            }
        case .tooManyRequests:
            if Locale.current.languageCode == "pl" {
                return "Wysłałeś zbyt wiele żądań do serwera. Proszę chwilę poczekać." } else {
                return "Sie haben zu viele Anfragen an den Server gesendet. Bitte etwas warten."
            }
        default:
            return nil
        }
    }
}

public extension Error {
    var localizedDescription: String {
        let error = self as NSError
        if error.domain == AuthErrorDomain {
            if let code = AuthErrorCode(rawValue: error.code) {
                if let errorString = code.description {
                    return errorString
                }
            }
        }
        return error.localizedDescription
    } }


来源:https://stackoverflow.com/questions/60351559/error-localizeddescription-always-returns-english

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!