How can I determine the language string in NSString? I try as follows:

NSString *text = @"text"; NSString *lang = (NSString *)CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage( (CFStringRef)text, CFRangeMake(0, text.length) )); 

null each time

Also tried this feature.

  • why not this example? stackoverflow.com/a/13783833 - BiMaWa
  • Yes, indeed this method works. But the language certainly does not quite correctly define) - Sergey Obraztsov

1 answer 1

approximate translation from here :

Cocoa has an API for checking the string language.

 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]; 

Another approach, if you have several languages ​​in the text, you can use enumerateLinguisticTagsInRange to get the language of a single word in the text.