damus-io / damus

iOS nostr client
GNU General Public License v3.0
2k stars 289 forks source link

iOS translation API on device #2302

Open alltheseas opened 3 months ago

alltheseas commented 3 months ago

@tyiu

https://www.macrumors.com/2024/06/20/apple-translation-framework/

benefits

-If iOS translations API works, this will save sats as compared to using current DeepL paid API. -privacy is increased, as no third party service outside iOS translation API is needed

performance

Unknown performance vs DeepL on: -speed/latency of translation -accuracy of translation -accuracy of language detection -language support

API documentation

https://developer.apple.com/documentation/Translation

Offer in-app translations with the Translation framework. You can use the built-in UI and let the system offer a translation to users on your behalf. Or you can use the framework to customize the translation experience.

To offer the built-in system translation experience, anchor the translationPresentation(isPresented:text:attachmentAnchor:arrowEdge:replacementAction:) view modifier to the SwiftUI view containing the text to translate. Set isPresented to true when you want to the built-in system translation UI to appear. Pass the text to translate to the text parameter.

To customize the translation experience use one of the translation tasks such as translationTask(_:action:). These functions provides you with a TranslationSession that you can use to translate strings of text one at a time, or in a batch. You can check language availability before offering a translation with the LanguageAvailability class.

Topics Essentials Translating text within your app Display simple system translations and create custom translation experiences. @MainActor @preconcurrency func translationPresentation(isPresented: Binding, text: String, attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds), arrowEdge: Edge = .top, replacementAction: ((String) -> Void)? = nil) -> some View Presents a translation popover when a given condition is true. @MainActor @preconcurrency func translationTask(_ configuration: TranslationSession.Configuration?, action: @escaping (TranslationSession) async -> Void) -> some View Adds a task to perform before this view appears or when the translation configuration changes. Beta @MainActor @preconcurrency func translationTask(source: Locale.Language? = nil, target: Locale.Language? = nil, action: @escaping (TranslationSession) async -> Void) -> some View Adds a task to perform before this view appears or when the specified source or target languages change. Beta class TranslationSession A class that performs translations between a pair of languages. Beta Availability class LanguageAvailability A check for language support and status. Beta Errors enum TranslationError Error codes describing why the framework can’t perform a translation. Beta

alltheseas commented 3 months ago

https://github.com/damus-io/damus/pull/2303

alltheseas commented 3 months ago

Looks like Terry was able to get this to work with iOS 17.4

alltheseas commented 3 months ago

Supported iOS languages via https://www.apple.com/ios/feature-availability/#translate-translate-app deepL via https://developers.deepl.com/docs/resources/supported-languages

@tyiu is it possible to mix & match deepL and iOS translation services to increase coverage?

Screenshot from 2024-06-21 08-22-03

alltheseas commented 3 months ago

@tyiu foremost it would be great to get Thai support added, as on nostr and Damus there is a decent Thai community.

Ideally automatic translation #1478 , so that we dont subject the user to taps each time they see a Thai note.

alltheseas commented 3 months ago

Moving to 2.0 to allow for sufficient time for testing in TestFlight