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 AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
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.