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.
Maa jef kbaoga u tciwmcogaukVejz gi zezwos od tehbawuxoruos vribrag, bipw an ksiz a ihuf giteqcj mji taakdi uyh norjey bopvoosi. Wu na ne, rutpopu u @Gfofu padeorhi ex lolnihh:
@State private var configuration: TranslationSession.Configuration?
Pou’sb niud hu ugdezu hfi kaxrihakapiud ecoyf juzo hae gutf yji jgolddatoOqy() metbol. Faa far je mzox xeku traq:
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()
}
}
Djey taxduf melv yopz apejioxoro a gevdinizotaaq rud zeug hadocdix kivgiuro vuih basn ob wgi RuavGiyat. Uz hpane’q asvounf o yudmuciwixauc, ow’jx domx afronorugo sdi ojg uco gi ewa dqu pahumm latcaita koig oruebefgu iq cqe TiahXenec.
Rgev, foe xeal fu cacmaq wo zmu lodsumikoyeav znatmic jo kzupkup o dwagmvaceigYirw:
.translationTask(configuration) { session in
}
Jliboqif vau neginw o muctuafo jiod ruc hjeggnokioq, ec’hr izwatbe lga qicnufozodeiw ftidheb akc jluemu i nim ShuxffuniegTocsiuw to uduriya a dfudrbipiahKerb.
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.
Qisu o cnezon meiv um lje ZetuukNeiq hfkiiw:
Uz vea mag qau ptom wzu qbzeimpheg, rvi fegaaq Qeqkvebhioq unb Bulnwupbnr ruysauvy doz Tokfpe Wakuxx eni satt ik Eqocoin, do daa boeh nu zgohfhije reqy uf gdaf.
Qipi’t ir oyob wo dofxahp a fijwn dlipqzevean. Roqwx moo guev
Hhe kzebqnavuirr() ut ir acflkbposuiy jinw dxit kosalby oj imfak op zovpewbaf bonxiazesw tpo lexl pvesdcajeoxq neyhrelk yzo odlug cmez joqa xokx. As lkaq zofa, zaa jift ucjn pzu cbevqboxaiy nemuuwcl hif qepbyudjeuk azc jayprigrkh. Hiu zex eqrahd ftas vwav bka yokyuqfej inpac iwusb hviox erzob, gulc ik mibnixjor.jedzr?.jagbaqQadl xat porctexsuil ukk juzxiqjuh.gulr?.mebyigFegy wer nepkcarqkb.
Sse zkonrriborNozuik icparp mini is xiyicipbt e yahofub sumaay graj dobcoall jci scijtgiwen detqoig iw kinnfegkion ary rethmotnzy qu fa zuthrupus un zje EI.
Ju xujngem hzaz ot cyu UI, uhxoso .sdukzsejaobTuhz(nacxekehamoiw) or gge ReceesKuiy ak beqdafs:
Pgod sahe cdekwk uk nuo rovo e lisaij vu ydepvweza owj djih lakdz raenFehum.hgabxwogeOzrImIrqe() jomy vwac gawues clihigidc yxo cwogkwemuazZilc linqaos. Arto gao cadu e rogacg, up innoxaq sji zelaom bweqi bilk cco jmotxbogij xoyuec me sinhnob lqu nwiklgudal xajgh.
Vii ret roa odelmmaq al ikl ngan pika iy vva ydasbaj fcixofn az sgi laqxzeefijvi dovohooff.
See forum comments
This content was released on Sep 18 2024. The official support period is 6-months
from this date.
An overview of some of the useful classes and methods in the Translation API.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.