Language detection is the first step towards interpreting it when you are presented with a text that you can't read or understand. We have apps like iTranslate, Google Translate, sayHi, iHandy which can translate most of the well-known languages for us. With rapid improvement, these apps not just translate between the languages of your choice but also detects the language as you start typing the text.

Language detection is not as easy as it seems to be unless you are asked to choose from a very few sample space. Because natural language is dynamic and two or more languages could be near similar.

And we need language detector because often times when working with user-provided text, we are not provided with language information. Google's ML Kit has come up with an api for language detection. This API can be used to determine the language of a string of text. There are a number of such apis provided by leading tech companies which more or less have the same capabilities. And ML Kit, specifically, has the following capabilities:

As of now, it can detect over a hundred different languages. The complete list is here.

It can identify Arabic, Bulgarian, Greek, Hindi, Japanese, Russian, and Chinese text in both native and romanized script.

On success, a BCP-47 language code is passed to the success listener, 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 value other than und only when it identifies the language with a confidence value of at least 0.5. You can change this threshold by passing a FirebaseLanguageIdentificationOptions object to getLanguageIdentification() as show below:

If the call succeeds, a list of IdentifiedLanguage objects is passed to the success listener. From each object, you can get the language's BCP-47 code and the confidence that the string is in that language. It is to be noted that ML Kit doesn't identify multiple languages in a single string. The values obtained indicate the confidence that the entire string is in the given language.

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