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 Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.
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.