Swift version: 5.6
The NSLinguisticTagger
class has dedicated code to help you identify the dominant language of a text string. Before I show you the code, there are three important provisos:
Here’s an example to help you get started:
let text = "Ceci n'est pas une pipe"
if let language = NSLinguisticTagger.dominantLanguage(for: text) {
print(language)
} else {
print("Unknown language")
}
When that code runs it will print “fr”, because the dominant language in the text is French.
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 11.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.