Andreas Prang Andreas Prang - 1 year ago 219
Objective-C Question

Detect Language of NSString

somebody told me about a Class for language recognition in Cocoa.
Does anybody know which one it is?

This is not working:

NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
[spellChecker setAutomaticallyIdentifiesLanguages:YES];
NSString *spellCheckText = @"Guten Tag Herr Mustermann. Dies ist ein deutscher Text. Bitte löschen Sie diesen nicht.";
[spellChecker checkSpellingOfString:spellCheckText startingAt:0];
NSLog(@"%@", [spellChecker language]);

The result is 'en' but should be 'de'.


Answer Source

Thats the result:

- (NSString *)languageForString:(NSString *) text{

     if (text.length < 100) {
         return (NSString *) CFStringTokenizerCopyBestStringLanguage((CFStringRef)text, CFRangeMake(0, text.length));
     } else {
         return (NSString *)CFStringTokenizerCopyBestStringLanguage((CFStringRef)text, CFRangeMake(0, 100));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download