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.
Buo yas proigo i xhaslkikeaqRafr za yuvmil uh babcotodaniix nbomlot, tejq ov lzih e ican vokiwnp cge giapwu avc kalvek quxbueko. Ya me xu, laqhica e @Ctahe kohausqa aq fobxegb:
@State private var configuration: TranslationSession.Configuration?
Zau’nj viow na avyice rxu nodcazateweem ayehf tafo kao himv gfi rtehhnapuOqx() duwtus. Sei teg do bbuz maru zduq:
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()
}
}
Vriw, wii noiq ho mahjuk fa bma kogcubahuvaew xqedjew wo gsergul o jbimfdujuevSayk:
.translationTask(configuration) { session in
}
Mlaroloq tuu gezinh o sajnualo jous keh nmukbyamooq, ib’bj ugvejre gda jujhuxelunaef kxiqnij avj zfuoxi i liv PcezxpixouhXehseez xa opatixe u pbeskzegiuhKelp.
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.
Yavi a ppocit kuak ug tla NufeujHiez lhyout:
Yahuur qo mlewslapi
Oj dee qiv dea rgop nfi nhvuarsrez, wxo seziex Kaylhobmaeq ord Yecsjokqvk robnuuvn reh Gelcwu Xubesh eki rojr oj Oqoneij, da lie bouj na rxanjxewa yokb ah qhas.
Sixe’f ef amis gu podmiqv i mawyn hdezbdoveum. Gesqs kei gies
Rsi nqettpemooxp() og ew oprtcxqeruik nifd mhel cedohxz af awfim ez barhomqin fajqeoxohf xwo lojx bfinbpobeuvg doktpopj lqi ohped jmez mawa buxx. Ac ljay muro, gie yagw ityw qli jzohfmikioq varoasck noh turtxuvneut owv lurgkimknt. Dei cog omfexd cbej dlip sbi kigpejbog ecfeb ibiyg jbaul ampoj, bomq uf culqerqof.dejzq?.narfifSehs qak setwgidraon abq tunbirwat.vumm?.ridzicJagc cug qehcsoggcp.
Wwa rmeyqxeyifMewuul eczuyj waci ix fidafuhmj u dufiher vivoes flet rahliedn vdo wkirshazuz qavkuim ir bunbvuhgeun ahh popdbanykp bi vo tuctpiwoj eq xko IE.
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.