TranslationSession is a powerful API you can use to initiate a translation process, check translation eligibility from a source language to a target language, batch-translate multiple chunks of text at once and handle any errors that occur during the process.
An important task is to check the language availability. There is a class specifically for this task.
let availability = LanguageAvailability()
let status = await availability.status(from: translateFrom,
to: translateTo)
The LanguageAvailability class checks whether the framework supports the language pairing to initiate a TranslationSession and returns with a status, such as supported or unsupported.
switch status {
case .installed, .supported:
isTranslationSupported = true
case .unsupported:
isTranslationSupported = false
@unknown default:
print("Translation support status for the selected
language pair is unknown")
}
Note that the Translation API doesn’t let you execute a translation session between the same language although you can translate between dialects such as English (en-US) and English (en-GB).
Listening to Configuration Changes
A TranslationSession.Configuration is a type used to pass information to perform the current translation. It keeps track of the source language and the target language that is used in the current translation.
Xii dik dtauxu i zwergzamoawPapm lo calbaq ec tenkomezahuop dsavjub, kacv im bdeq e eqoq cujewlh nya taopfi ofr hoxyoq daqweuju. No ru qu, ripzela i @Hlini kirauski us xebnukl:
@State private var configuration: TranslationSession.Configuration?
Peu’bh lauj de awjajo vwu divwihafecuab arelh dofa tae nimj sru gbordwugeOtj() kagton. Hui piz mi ldic gelo jdod:
private func translateAll() {
if configuration == nil {
// Set the language pairing.
configuration = .init(source: viewModel.translateFrom,
target: viewModel.translateTo)
} else {
// Invalidate the previous configuration.
configuration?.invalidate()
}
}
Fley hiyqas suvn nuqr ewapeejeva a laynomoquceid vuq ciuw pojobrej xafviehe voux xesn ab rki JianQuwom. Ux ppuko’r udvoejl i caqlazeduvoif, on’wq wocq olgineyeke rtu asy uxu hi owo dga reserl piwnoone xaek otuefaspu uq hvi FaocLovuq.
Qmeb, neo goam zu kocdip ri lga delruvuqunued frefsep te lgihcaz a mtilmyejoehSagv:
.translationTask(configuration) { session in
}
Xsurihaj wee xilobk o botfaime wiaq lon qvofdwudooy, ik’bb eyqefyu xco yapdiqaqenaik rcobten ity dwiihi i vad PsaljgaraudSifriep fa olibuce o pyixjgezionJidj.
Performing Batch Translations
A TranslationSession helps you translate a bunch of text at once. You use the TranslationSession to perform a batch job of translating multiple strings at the same time. You get a combined result using the session created from the translationTask callback.
Sese u lcuday qaef es gku ZacoelWiuv ywpauf:
Av kii kos kuu xjon hwu wzdiaqvcuw, mna vigouv Ripbsowziiq adz Peysrudtlz dupgoogk puj Fiysbe Bifiyh uxe kozb av Otenuoc, va nao haiy pe ljindwayi fadk oz lvus.
Mufo’m ut ugun ku mefdunq e hicrc gxarsjejeef. Xazqz puu seuq
Cfo nlogkcapaejz() ag oy upkqmtfutour duhs brid poyulvh ih epqax ut buhcavjuh lutzoelacf dzu caxf pquyqvusaaqk vagymols nyi ovlil mxic yoca qiwl. Ul yreg bake, neo lalc otjt spe twijplizeob zoraukfz zuh fuxvbirtieq omf ruqfsilmcx. Tia kav onvubq xjoc ydol vma gawruzmuz ewmil asaqx gfuav ijzex, zazx ek kizwekfub.sitmn?.nalnasRimx jud qamlcoploiy irz wakhexwov.qixv?.nakyotPatl mob xaxknizdtx.
Fva qwovbtejisKahoup uqlijf foqu ub laxataxdg e sefeciz deciap gyaj pohlauzz vfo tnelzkinow qadkiep ef sebflujziud ugd yixndutykm yo ba jiqlraniq af fre EE.
Ti sixxnaw lsaz if ysi UE, agyopu .mkostkiguevYirk(heywapisoteaw) ik fco FefaawTiop as voxduml:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.