Share
Embed
Meet the Translation API
WWDC24で発表された「Meet the Translation API」を要約した資料です。
Meet the Translation API
・https://developer.apple.com/videos/play/wwdc2024/10117
Translating text within your app
・https://developer.apple.com/documentation/translation/translating-text-within-your-app
akkiee76
June 26, 2024
Transcript
20 slides
-
#love_swift ©2024 RAKUS Co., Ltd. Translating text within your app
集まれSwift好き!Swift愛好会スピンオフ WWDC24セッション要約会 @ DeNA 2024/06/26 @akkiee76 -
#love_swift • Akihiko Sato • DeNA Co., Ltd. • 𝕏
: @akkiee76 • Speaker Deck : @akkie76 Profile 2 -
#love_swift Overview • Adopt “.translationPresentation()” to quickly add translation to
your app. • User TranslateSession for your deeper integration with your app’s UI. • Best Practices 5 -
#love_swift Translate Framework You can offer in-app translations from one
language to another. To display simple system translations in your app. 🚀 6 -
#love_swift Simple translation overlay • .translationPresentation API ◦ Presents a
translation popover when a given condition is true. 8 var body: some View { VStack { Text(verbatim: originalText) Button("Translate") { showTranslation.toggle() } } // Offer a system UI translation. .translationPresentation(isPresented: $showTranslation, text: originalText) SwiftUI | iOS 17.4+ | iPadOS 17.4+ | macOS 14.4+ -
#love_swift Flexible translation API • TranslationSession 10 OS 18.0+ Beta
| iPadOS 18.0+ Beta | macOS 15.0+ Beta VStack { TextField("Enter text to translate", text: $sourceText) } // Pass the configuration to the task. .translationTask { session in do { // Use the session the task provides to translate the text. let response = try await session.translate(sourceText) // Update the view with the translated result. targetText = response.targetText } catch { // Handle any errors. } } -
#love_swift Flexible translation API 11 @State private var configuration: TranslationSession.Configuration?
var body: some View { VStack { TextField("Enter text to translate", text: $sourceText) Button("Translate") { configuration = .init() } Text(verbatim: targetText) } // Pass the configuration to the task. .translationTask(configuration) { session in do { // Use the session the task provides to translate the text. let response = try await session.translate(sourceText) // Update the view with the translated result. targetText = response.targetText } catch { } -
#love_swift Flexible translation API 12 @State private var configuration: TranslationSession.Configuration?
var body: some View { VStack { TextField("Enter text to translate", text: $sourceText) Button("Translate") { triggerTranslation() } Text(verbatim: targetText) } // Pass the configuration to the task. .translationTask(configuration) { session in do { // Use the session the task provides to translate the text. let response = try await session.translate(sourceText) // Update the view with the translated result. targetText = response.targetText } catch { } func triggerTranslation() { guard configuration == nil else { configuration?.invalidate() return } // Let the framework automatically determine the language pairing. configuration = .init() } -
#love_swift On-device translation • On-device ML models • Shared with
all apps, including Translate • API take care of : ◦ Download permission ◦ Download progress ◦ Continuing in background 13 -
#love_swift Translating multiple languages • Badge translation API ◦ Translates
multiple strings of text of the same language, returning the results all at once when complete. 14 public func translate(batch: [TranslationSession.Request]) -> TranslationSession.BatchResponse iOS 18.0+ | iPadOS 18.0+ | macOS 15.0+ public func translations(from batch: [TranslationSession.Request]) async throws -> [TranslationSession.Response] -
#love_swift Translating multiple languages 15 var foodItems = ["Salat 🥗",
"Fritten 🍟", "Suppe 🍜"] func translateAllAtOnce(using session: TranslationSession) async { Task { @MainActor in let requests: [TranslationSession.Request] = foodItems.map { // Map each item into a request. TranslationSession.Request(sourceText: $0) } do { let responses = try await session.translations(from: requests) foodItems = responses.map { // Update each item with the translated result. $0.targetText } } catch { // Handle any errors. } } } -
#love_swift Translating multiple languages 16 func translate(using session: TranslationSession) async
{ do { var requesets: [TranslationSession.Request] = [] requesets.append(contentsOf: self.germanRequests) equesets.append(contentsOf: self.spanishRequests) for try await response in session.translate(batch: requesets) { handle(response: response) } } catch { // Handle any errors. } } Do not merge sessions of different languages -
#love_swift Translating multiple languages 17 func translate(using session: TranslationSession) async
{ do { for try await response in session.translate(batch: self.germanRequests) { handle(response: response) } for try await response in session.translate(batch: self.spanishRequests) { handle(response: response) } } catch { // Handle any errors. } } ✅ -
#love_swift Others • Language Support ◦ Hindi • Best Practice
◦ Not Simulator ◦ SF Symbols translate 18 -
#love_swift Preferences • Meet the Translation API ◦ https://developer.apple.com/videos/play/wwdc2024/10117 •
Translating text within your app ◦ https://developer.apple.com/documentation/translation/transl ating-text-within-your-app 19