Swift version: 5.2
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 ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.
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.