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:

  • Why not just set initial view controller for login screen like this?
  • How to pass data from child to parent view controller? in swift
  • Is there a way to differentiate between left and right Shift keys being pressed?
  • Programmatically select text in UIWebView
  • Calling a phone number in swift
  • Is it possible to exclude 3.5 inch screen devices when submitting an iOS app?
  • 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’.

    Thanks…

    4 Solutions Collect From Internet About “Detect Language of NSString”

    There is API in cocoa available to check the language of a string, and it is always best to use Foundation over CoreFoundation whenever possible.

    NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
    NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
    [tagger setString:@"Das ist ein bisschen deutscher Text. Bitte löschen Sie diesen nicht."];
    NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
    

    Alternatively, if you happen to have mixed language text, you can use the enumerateLinguisticTagsInRange API to get the language of each word in the text.

    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));
         }
    }
    

    You can use -requestCheckingOfString:… instead. NSTextCheckingTypeOrthography attempts to identify the language used in the string, and the completion handler receives an NSOrthography parameter that can be used to get information about the orthography in the string, including its dominant language.

    The following example outputs dominant language = de:

    NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
    [spellChecker setAutomaticallyIdentifiesLanguages:YES];
    NSString *spellCheckText = @"Guten Herr Mustermann. Dies ist ein deutscher Text. Bitte löschen Sie diesen nicht.";
    
    [spellChecker requestCheckingOfString:spellCheckText
        range:(NSRange){0, [spellCheckText length]}
        types:NSTextCheckingTypeOrthography
        options:nil
        inSpellDocumentWithTag:0
        completionHandler:^(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount) {
            NSLog(@"dominant language = %@", orthography.dominantLanguage);
    }];
    

    A swift String extension for Jennifer’s answer:

    extension String {
        func language() -> String? {
            let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeLanguage], options: 0)
            tagger.string = self
            return tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil)
        }
    }
    

    Usage:

    let language = "What language is this?".language()