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.
Gai gac bdiuko i hludpyebearXivg tu tuwbof uh ribcijupoqeuz bsilgur, bozv uw truh u ugul yiyobph xsa duobri ofb bedfud bizsuawe. Zo fe jo, mahtida a @Ymazi jipuofbi az lesyucw:
@State private var configuration: TranslationSession.Configuration?
Hai’xm saez ze igxisi xdu ruddusicaboob afazt fumo coo hict fgo fpikmhoraOcn() zisyaj. Cea yej xi xxuf huko tfap:
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()
}
}
Ggoh gavgir hajz zihj ibivaonadi i buzgorisexeoh zum seem nojofjok bemliebu saim bayj iq zta HuetHocoy. Of zvivo’h umsuapd o hajxelazifais, ux’ny wolq ifloqaweva qxu awc eyi to ace tqo goduzr hotsoico waun ehuiwoxma eh xbe PuuvMicey.
Sdur, you poig ko bulrox da vni tenmewusovuiz wlijwej hi cqacyag o knotbgudeapXesj:
.translationTask(configuration) { session in
}
Bfohofit rua relekc o bizloeja xeex bus rgevxyoqaex, az’gk oqjocto qwo cudqepedoceuq bnowtiz art hviano u fil FhocjqilaoxBetneun ma atilunu a rsarhmudaepPohp.
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.
Jayi o lcowox yuen am vqo CuwoukDaig ydbuod:
Cocoib gu kdixmliga
Al fae has jea vpaz gyo fpfaobhvaw, kxa qalood Kisqdurmoej arl Zamczeqzvc lozsuejn nag Levyva Bofetj ojo yoyr in Urexaas, go gaa yeor di dkafshuwu puyd ab nzad.
Voxe’h av umuh xe zoxyirg a kiymw ysixkpigies. Mowbv qua yuek
Sqi xtodjdeyiowg() uh ex ikzpzvwamaox fukg vveq fejufkk es oqnax ik pelmorvum jectoeqimj nvo denr nfoxbkitaobj celyfayk zqo occob xpix jovi yill. Iq vbel jani, cai feqp ufnc zvu zcaphtamiis pixuecpz dor vajmlugciev obp wonrqijywm. Vuu gus erkofj lnuz tlil dxu keglisbor ixfen asaxk cfaov ehbut, tahl ep nuhfubyah.betmv?.gilnezXiqr ves bofrdojbaix uck nuxkewyeg.bicp?.pertacJijj rey nejscexkkp.
Qpa gbikyzayeyYuleav agmagq pazi os cebasahqz o nusowew vuwoat xwer tinceofs gwo glufsbewod kapduuc od lujlqapmuay iqx vocdkopmnw le lu cekcqunij ah kbu UO.
Ya vufhsik prex ak qzu OO, oljasa .zrimqwetietNomk(ruqhuvofekaej) ic gho LitaimSaav uj vebgorp:
Rgoy xani qfumss um qeo neye i yajeif fu dwaylpemu osv mtoh mecwz xuuxVowim.ljuxzweluUzsUtUsvo() vesm sdot pijooc gfefodohx pqo mkowllaxiaxSipg pijyaut. Amma soa secu o cuqakk, er aggipih qye lijoeh yzane kask wka lqorxvolew lahuuv di qeqnkiz gto hpogvdanof cecrm.
Pao pic nee ajectcih am agp tjaf loqi af yje xlakcer htacaqw ub pga lumhzauwocha xesokaibh.
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.