Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hyuni/d64dedad1f72e1c77adead628f7c8c33 to your computer and use it in GitHub Desktop.
Save hyuni/d64dedad1f72e1c77adead628f7c8c33 to your computer and use it in GitHub Desktop.
Detect string language with Swift
//: Playground - noun: a place where people can play
//
// The result is not guaranteed to be accurate. Typically, the function requires 200-400 characters to reliably guess the language of a string.
// Reference: [CFStringTokenizerCopyBestStringLanguage(_:_:)](https://developer.apple.com/reference/corefoundation/1542136-cfstringtokenizercopybeststringl)
//
import Foundation
extension String {
func guessLanguage() -> String {
let length = self.utf16.count
let languageCode = CFStringTokenizerCopyBestStringLanguage(self as CFString, CFRange(location: 0, length: length)) as String? ?? ""
let locale = Locale(identifier: languageCode)
return locale.localizedString(forLanguageCode: languageCode) ?? "Unknown"
}
}
let names = ["明日香", "碇真嗣", "綾波零", "拓跋玉兒", "陳靖仇", "于小雪"]
for name in names {
print("\(name) - \(name.guessLanguage())")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment