Identify the language of text with ML Kit on iOS

  • ML Kit can identify the language of a string of text and provide confidence scores for all possible languages, supporting over 100 languages.

  • You can get the most likely language of a string using the identifyLanguage(for:) method or get confidence values for possible languages using the identifyPossibleLanguages(for:) method.

  • Adjust the confidence threshold for language identification by passing a LanguageIdentificationOptions object to languageIdentification(options:).

  • To use ML Kit for language identification, include the GoogleMLKit/LanguageID pod in your Podfile and ensure your Xcode version is 12.4 or greater.

You can use ML Kit to identify the language of a string of text. You can get the string's most likely language as well as confidence scores for all of the string's possible languages.

ML Kit recognizes text in more than 100 different languages in their native scripts. In addition, romanized text can be recognized for Arabic, Bulgarian, Chinese, Greek, Hindi, Japanese, and Russian. See the complete list of supported languages and scripts.

Try it out

  • Play around with the sample app to see an example usage of this API.

Before you begin

  1. Include the following ML Kit pods in your Podfile:
    pod 'GoogleMLKit/LanguageID', '8.0.0'
    
  2. After you install or update your project's Pods, open your Xcode project using its .xcworkspace. ML Kit is supported in Xcode version 12.4 or greater.

Identify the language of a string

To identify the language of a string, get an instance of LanguageIdentification, and then pass the string to the identifyLanguage(for:) method.

For example:

Swift

letlanguageId=NaturalLanguage.languageIdentification()
languageId.identifyLanguage(for:text){(languageCode,error)in
ifleterror=error{
print("Failed with error: \(error)")
return
}
ifletlanguageCode=languageCode,languageCode!="und"{
print("Identified Language: \(languageCode)")
}else{
print("No language was identified")
}
}

Objective-C

MLKLanguageIdentification*languageId=[MLKLanguageIdentificationlanguageIdentification];
[languageIdidentifyLanguageForText:text
completion:^(NSString*_NullablelanguageCode,
NSError*_Nullableerror){
if(error!=nil){
NSLog(@"Failed with error: %@",error.localizedDescription);
return;
}
if(![languageCodeisEqualToString:@"und"]){
NSLog(@"Identified Language: %@",languageCode);
}else{
NSLog(@"No language was identified");
}
}];

If the call succeeds, a BCP-47 language code is passed to the completion handler, indicating the language of the text. If no language could be confidently detected, the code und (undetermined) is passed.

By default, ML Kit returns a non-und value only when it identifies the language with a confidence value of at least 0.5. You can change this threshold by passing a LanguageIdentificationOptions object to languageIdentification(options:):

Swift

letoptions=LanguageIdentificationOptions(confidenceThreshold:0.4)
letlanguageId=NaturalLanguage.languageIdentification(options:options)

Objective-C

MLKLanguageIdentificationOptions*options=
[[MLKLanguageIdentificationOptionsalloc]initWithConfidenceThreshold:0.4];
MLKLanguageIdentification*languageId=
[MLKLanguageIdentificationlanguageIdentificationWithOptions:options];

Get the possible languages of a string

To get the confidence values of a string's most likely languages, get an instance of LanguageIdentification and then pass the string to the identifyPossibleLanguages(for:) method.

For example:

Swift

letlanguageId=NaturalLanguage.languageIdentification()
languageId.identifyPossibleLanguages(for:text){(identifiedLanguages,error)in
ifleterror=error{
print("Failed with error: \(error)")
return
}
guardletidentifiedLanguages=identifiedLanguages,
!identifiedLanguages.isEmpty,
identifiedLanguages[0].languageCode!="und"
else{
print("No language was identified")
return
}
print("Identified Languages:\n"+
identifiedLanguages.map{
String(format:"(%@, %.2f)",0ドル.languageCode,0ドル.confidence)
}.joined(separator:"\n"))
}

Objective-C

MLKLanguageIdentification*languageId=[MLKLanguageIdentificationlanguageIdentification];
[languageIdidentifyPossibleLanguagesForText:text
completion:^(NSArray*_NonnullidentifiedLanguages,
NSError*_Nullableerror){
if(error!=nil){
NSLog(@"Failed with error: %@",error.localizedDescription);
return;
}
if(identifiedLanguages.count==1
&&[identifiedLanguages[0].languageCodeisEqualToString:@"und"]){
NSLog(@"No language was identified");
return;
}
NSMutableString*outputText=[NSMutableStringstringWithFormat:@"Identified Languages:"];
for(MLKIdentifiedLanguage*languageinidentifiedLanguages){
[outputTextappendFormat:@"\n(%@, %.2f)",language.languageCode,language.confidence];
}
NSLog(outputText);
}];

If the call succeeds, a list of IdentifiedLanguage objects is passed to the continuation handler. From each object, you can get the language's BCP-47 code and the confidence that the string is in that language. Note that these values indicate the confidence that the entire string is in the given language; ML Kit doesn't identify multiple languages in a single string.

By default, ML Kit returns only languages with confidence values of at least 0.01. You can change this threshold by passing a LanguageIdentificationOptions object to languageIdentification(options:):

Swift

letoptions=LanguageIdentificationOptions(confidenceThreshold:0.4)
letlanguageId=NaturalLanguage.languageIdentification(options:options)

Objective-C

MLKLanguageIdentificationOptions*options=
[[MLKLanguageIdentificationOptionsalloc]initWithConfidenceThreshold:0.4];
MLKLanguageIdentification*languageId=
[MLKLanguageIdentificationlanguageIdentificationWithOptions:options];

If no language meets this threshold, the list has one item, with the value und.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年09月01日 UTC.