๐ Translating Text into Another Language Using Just a Single Line of Code ๐ฑ
Try out Xcode 26 in your GitHub Actions with Cirrus Runners
Enjoy the fastest Apple Silicon M4 Pro chips at a fixed monthly price โ fully managed so you can focus on shipping great code. Start your free 10-day trial and get 50% off your first three months with code WWDC50.
This message is brought to you by a sponsor who helps keep this content free for everyone. If you have a moment, check them out - your support means a lot!
Welcome to issue #53 of the iOS Coffee Break Newsletter ๐ฌ and to the 2nd edition of the "Get started with Machine Learning" series.
In the last issue of the newsletter, I introduced a new series titled "Get Started with Machine Learning", which - no surprises here ๐คญ - is all about exploring various machine learning methods.
In this week's edition, I'll demonstrate how effortlessly we can implement text translation using Apple's new Translation framework. If we are working with SwiftUI, it takes just a SINGLE line of code to make it happen. Sounds too good to be true right? ๐ฑ
Read on and see for yourself! ๐
Translating Text to Another Language
Let's kick off by exploring how we can use Apple's Translation API.
To start, here is how it works:
import SwiftUI
import Translation
struct TranslationView: View {
@State private var text = ""
@State private var showingTranslation = false
var body: some View {
NavigationStack {
List {
Section {
TextField(
"Enter the text to translate",
text: $text,
axis: .vertical
)
}
}
.navigationTitle("Translation Playground")
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Translate") {
showingTranslation = true
}
.disabled(text.isEmpty)
}
}
// the line that makes all possible!
.translationPresentation(isPresented: $showingTranslation, text: text)
}
}
}
Here is a brief rundown of what the code accomplishes:
- We import Apple's Translation machine learning framework and apply the
.translationPresentation
view modifier. - This modifier brings up a sheet that displays the original text and its detected language, lets users choose a target language, and shows the translated version of the text. It also has the ability to read the translated text out loud. You can either rely on the built-in interface to let the system handle translations for your users, or use the framework to build a more tailored translation experience.
To dive deeper into what the Translation framework can do, explore this guide.
To try it out, tap the Translate
button. Experiment with different languages and use the play icon to listen to how each translation is pronounced.
P.S. Apologies if the Chinese text I pasted isn't quite right! ๐
๐ค Wrapping Up
In this edition, we explored how the Translation framework leverages machine learning to deliver powerful translation capabilities. Although it is possible to perform translations without machine learning, this framework streamlines the process, making it both easier and more effective.
Have any feedback, suggestions, or ideas to share? Feel free to reach out to me on Twitter.