feat: Implement error-handling for translation

This commit is contained in:
Marcus Kida 2022-12-09 16:12:13 +01:00
parent fda3ae1516
commit 58dcadf642
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
6 changed files with 62 additions and 15 deletions

View File

@ -51,6 +51,11 @@
"clean_cache": {
"title": "Clean Cache",
"message": "Successfully cleaned %s cache."
},
"translation_failed": {
"title": "Note",
"message": "Translation failed. Maybe the administrator has not enabled translations on this instance or this instance is running an older version of Mastodon where translations are not yet supported.",
"button": "OK"
}
},
"controls": {

View File

@ -393,12 +393,18 @@ extension DataSourceFacade {
alertController.addAction(cancelAction)
dependency.present(alertController, animated: true)
case let .translateStatus(translationContext):
case .translateStatus:
guard let status = menuContext.status else { return }
try await DataSourceFacade.translateStatus(
provider: dependency,
status: status
)
do {
try await DataSourceFacade.translateStatus(
provider: dependency,
status: status
)
} catch TranslationFailure.emptyOrInvalidResponse {
let alertController = UIAlertController(title: L10n.Common.Alerts.TranslationFailed.title, message: L10n.Common.Alerts.TranslationFailed.message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: L10n.Common.Alerts.TranslationFailed.button, style: .default))
dependency.present(alertController, animated: true)
}
}
} // end func
}

View File

@ -11,6 +11,10 @@ import CoreDataStack
import MastodonCore
extension DataSourceFacade {
enum TranslationFailure: Error {
case emptyOrInvalidResponse
}
public static func translateStatus(
provider: UIViewController & NeedsDependency & AuthContextProvider,
status: ManagedObjectRecord<Status>
@ -25,19 +29,37 @@ extension DataSourceFacade {
}
func translate(status: Status) async throws -> String? {
let value = try await provider.context
.apiService
.translateStatus(
statusID: status.id,
authenticationBox: provider.authContext.mastodonAuthenticationBox
).value
return value.content
}
do {
let value = try await provider.context
.apiService
.translateStatus(
statusID: status.id,
authenticationBox: provider.authContext.mastodonAuthenticationBox
).value
guard let content = value.content else {
throw TranslationFailure.emptyOrInvalidResponse
}
return content
} catch {
throw TranslationFailure.emptyOrInvalidResponse
}
}
func translateAndApply(to status: Status) async throws {
do {
status.translatedContent = try await translate(status: status)
} catch {
status.translatedContent = nil
throw TranslationFailure.emptyOrInvalidResponse
}
}
if let reblog = status.reblog {
reblog.translatedContent = try await translate(status: reblog)
try await translateAndApply(to: reblog)
} else {
status.translatedContent = try await translate(status: status)
try await translateAndApply(to: status)
}
}
}

View File

@ -87,6 +87,14 @@ public enum L10n {
/// Sign Up Failure
public static let title = L10n.tr("Localizable", "Common.Alerts.SignUpFailure.Title", fallback: "Sign Up Failure")
}
public enum TranslationFailed {
/// OK
public static let button = L10n.tr("Localizable", "Common.Alerts.TranslationFailed.Button", fallback: "OK")
/// Translation failed. Maybe the administrator has not enabled translations on this instance or this instance is running an older version of Mastodon where translations are not yet supported.
public static let message = L10n.tr("Localizable", "Common.Alerts.TranslationFailed.Message", fallback: "Translation failed. Maybe the administrator has not enabled translations on this instance or this instance is running an older version of Mastodon where translations are not yet supported.")
/// Note
public static let title = L10n.tr("Localizable", "Common.Alerts.TranslationFailed.Title", fallback: "Note")
}
public enum VoteFailure {
/// The poll has ended
public static let pollEnded = L10n.tr("Localizable", "Common.Alerts.VoteFailure.PollEnded", fallback: "The poll has ended")

View File

@ -22,6 +22,9 @@ Please check your internet connection.";
"Common.Alerts.SignOut.Message" = "Are you sure you want to sign out?";
"Common.Alerts.SignOut.Title" = "Sign Out";
"Common.Alerts.SignUpFailure.Title" = "Sign Up Failure";
"Common.Alerts.TranslationFailed.Title" = "Note";
"Common.Alerts.TranslationFailed.Message" = "Translation failed. Maybe the administrator has not enabled translations on this instance or this instance is running an older version of Mastodon where translations are not yet supported.";
"Common.Alerts.TranslationFailed.Button" = "OK";
"Common.Alerts.VoteFailure.PollEnded" = "The poll has ended";
"Common.Alerts.VoteFailure.Title" = "Vote Failure";
"Common.Controls.Actions.Add" = "Add";

View File

@ -22,6 +22,9 @@ Please check your internet connection.";
"Common.Alerts.SignOut.Message" = "Are you sure you want to sign out?";
"Common.Alerts.SignOut.Title" = "Sign Out";
"Common.Alerts.SignUpFailure.Title" = "Sign Up Failure";
"Common.Alerts.TranslationFailed.Title" = "Note";
"Common.Alerts.TranslationFailed.Message" = "Translation failed. Maybe the administrator has not enabled translations on this instance or this instance is running an older version of Mastodon where translations are not yet supported.";
"Common.Alerts.TranslationFailed.Button" = "OK";
"Common.Alerts.VoteFailure.PollEnded" = "The poll has ended";
"Common.Alerts.VoteFailure.Title" = "Vote Failure";
"Common.Controls.Actions.Add" = "Add";