FirebaseLanguageIdentificationlanguageIdentifier=FirebaseNaturalLanguage.getInstance().getLanguageIdentification();languageIdentifier.identifyLanguage(text).addOnSuccessListener(newOnSuccessListener<String>(){@OverridepublicvoidonSuccess(@NullableStringlanguageCode){if(languageCode!="und"){Log.i(TAG,"Language: "+languageCode);}else{Log.i(TAG,"Can't identify language.");}}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Model couldn’t be loaded or other internal error.// ...}});
如果呼叫成功,
BCP-47 語言代碼是
會傳遞到成功事件監聽器,表示文字的語言。詳情請參閱
支援語言的完整清單。如果答案為「否」
系統就能正確偵測語言,就會傳遞 und (未判定) 程式碼。
根據預設,ML Kit 只會在偵測到 und 時傳回 und 以外的值
信心值至少為 0.5 的語言您可以調整這項設定
傳送 FirebaseLanguageIdentificationOptions 物件給
getLanguageIdentification():
FirebaseLanguageIdentificationlanguageIdentifier=FirebaseNaturalLanguage.getInstance().getLanguageIdentification();languageIdentifier.identifyAllLanguages(text).addOnSuccessListener(newOnSuccessListener<String>(){@OverridepublicvoidonSuccess(List<IdentifiedLanguage>identifiedLanguages){for(IdentifiedLanguageidentifiedLanguage:identifiedLanguages){Stringlanguage=identifiedLanguage.getLanguageCode();floatconfidence=identifiedLanguage.getConfidence();Log.i(TAG,language+" ("+confidence+")");}}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Model couldn’t be loaded or other internal error.// ...}});
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["You can use ML Kit to identify the language of a string of text. You can\nget the string's most likely language or get confidence scores for all of the\nstring's possible languages.\n\nML Kit recognizes text in 103 different languages in their native scripts.\nIn addition, romanized text can be recognized for Arabic, Bulgarian, Chinese,\nGreek, Hindi, Japanese, and Russian.\n\n\u003cbr /\u003e\n\nBefore you begin\n\n1. If you haven't already, [add Firebase to your Android project](/docs/android/setup).\n2. Add the dependencies for the ML Kit Android libraries to your module (app-level) Gradle file (usually `app/build.gradle`): \n\n ```carbon\n apply plugin: 'com.android.application'\n apply plugin: 'com.google.gms.google-services'\n\n dependencies {\n // ...\n\n implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0'\n implementation 'com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7'\n }\n ```\n\nIdentify the language of a string\n\nTo identify the language of a string, get an instance of\n`FirebaseLanguageIdentification`, and then pass the string to the\n`identifyLanguage()` method.\n\nFor example: \n\n FirebaseLanguageIdentification languageIdentifier =\n FirebaseNaturalLanguage.getInstance().getLanguageIdentification();\n languageIdentifier.identifyLanguage(text)\n .addOnSuccessListener(\n new OnSuccessListener\u003cString\u003e() {\n @Override\n public void onSuccess(@Nullable String languageCode) {\n if (languageCode != \"und\") {\n Log.i(TAG, \"Language: \" + languageCode);\n } else {\n Log.i(TAG, \"Can't identify language.\");\n }\n }\n })\n .addOnFailureListener(\n new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n // Model couldn't be loaded or other internal error.\n // ...\n }\n });\n\nIf the call succeeds, a\n[BCP-47 language code](//en.wikipedia.org/wiki/IETF_language_tag) is\npassed to the success listener, indicating the language of the text. See the\n[complete list of supported languages](/docs/ml-kit/langid-support). If no\nlanguage could be confidently detected, the code `und` (undetermined) is passed.\n\nBy default, ML Kit returns a value other than `und` only when it identifies\nthe language with a confidence value of at least 0.5. You can change this\nthreshold by passing a `FirebaseLanguageIdentificationOptions` object to\n`getLanguageIdentification()`: \n\n FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage\n .getInstance()\n .getLanguageIdentification(\n new FirebaseLanguageIdentificationOptions.Builder()\n .setIdentifyLanguageConfidenceThreshold(0.34f)\n .build());\n\nGet the possible languages of a string\n\nTo get the confidence values of a string's most likely languages, get an\ninstance of `FirebaseLanguageIdentification`, and then pass the string to the\n`identifyAllLanguages()` method.\n\nFor example: \n\n FirebaseLanguageIdentification languageIdentifier =\n FirebaseNaturalLanguage.getInstance().getLanguageIdentification();\n languageIdentifier.identifyAllLanguages(text)\n .addOnSuccessListener(\n new OnSuccessListener\u003cString\u003e() {\n @Override\n public void onSuccess(List\u003cIdentifiedLanguage\u003e identifiedLanguages) {\n for (IdentifiedLanguage identifiedLanguage : identifiedLanguages) {\n String language = identifiedLanguage.getLanguageCode();\n float confidence = identifiedLanguage.getConfidence();\n Log.i(TAG, language + \" (\" + confidence + \")\");\n }\n }\n })\n .addOnFailureListener(\n new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception e) {\n // Model couldn't be loaded or other internal error.\n // ...\n }\n });\n\nIf the call succeeds, a list of `IdentifiedLanguage` objects is passed to the\nsuccess listener. From each object, you can get the language's BCP-47 code and\nthe confidence that the string is in that language. See the\n[complete list of supported languages](/docs/ml-kit/langid-support). Note that\nthese values indicate the confidence that the entire string is in the given\nlanguage; ML Kit doesn't identify multiple languages in a single string.\n\nBy default, ML Kit returns only languages with confidence values of at least\n0.01. You can change this threshold by passing a\n`FirebaseLanguageIdentificationOptions` object to\n`getLanguageIdentification()`: \n\n FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage\n .getInstance()\n .getLanguageIdentification(\n new FirebaseLanguageIdentificationOptions.Builder()\n .setIdentifyAllLanguagesConfidenceThreshold(0.5f)\n .build());\n\nIf no language meets this threshold, the list will have one item, with the value\n`und`."]]