Site icon C1CTech

Translate text with ML Kit on Android

&NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div>This post is about how to setup ML Kit’s translation feature in Android application with the help of simple demo app&period;<&sol;div> &NewLine;<div> <&sol;div> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;030560&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;TranslateTextExp"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"Hrn8eec59Mg" title&equals;"Translate text with ML Kit on Android"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;Hrn8eec59Mg"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;Hrn8eec59Mg&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Translate text with ML Kit on Android"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>1 &period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><b>File <&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;"> New Android Project<&sol;span>&comma;<&sol;b> select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span> &comma; provide <span style&equals;"color&colon; &num;008000&semi;"><strong>app name<&sol;strong><&sol;span>&comma; select language to <strong><span style&equals;"color&colon; &num;008000&semi;">kotlin<&sol;span> <&sol;strong>and then finally click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Adding Dependency<&sol;span><&sol;strong><&sol;h5> &NewLine;<p>2 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">build&period;gradle &lpar;Module&colon; app&rpar;<&sol;span><&sol;strong><&sol;span> file and add the ML Kit’s Translation API dependency inside the <span style&equals;"color&colon; &num;008000&semi;"><strong>dependencies<&sol;strong><&sol;span> section&colon;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">build&period;gradle<&sol;span><&sol;strong><&sol;span><&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;ml kit translation API<&sol;span><&sol;strong><br &sol;> implementation 'com&period;google&period;mlkit&colon;translate&colon;16&period;1&period;2'<br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Layout File<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>3&period; The <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> layout file defines the UI of the application&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;margin&equals;"10dp"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;from&lowbar;english"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;text&equals;"From English"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;700"<br &sol;> android&colon;textSize&equals;"20sp"<br &sol;> android&colon;textStyle&equals;"bold"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;EditText<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;edt&lowbar;source&lowbar;lang&lowbar;text"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"10dp"<br &sol;> android&colon;hint&equals;"Enter English text"<br &sol;> android&colon;textSize&equals;"18sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;from&lowbar;english"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;from&lowbar;english" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;to&lowbar;hindi"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;text&equals;"To Hindi"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;700"<br &sol;> android&colon;textSize&equals;"20sp"<br &sol;> android&colon;textStyle&equals;"bold"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;edt&lowbar;source&lowbar;lang&lowbar;text"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;edt&lowbar;source&lowbar;lang&lowbar;text" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;target&lowbar;lang&lowbar;text"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"10dp"<br &sol;> android&colon;hint&equals;"Hindi Translation"<br &sol;> android&colon;textSize&equals;"20sp"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;to&lowbar;hindi"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;to&lowbar;hindi" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;ProgressBar<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;progressBar"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;padding&equals;"10dp"<br &sol;> android&colon;visibility&equals;"gone"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;target&lowbar;lang&lowbar;text" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;translate"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;padding&equals;"10dp"<br &sol;> android&colon;text&equals;"Translate"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;target&lowbar;lang&lowbar;text" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Writing MainActivity Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p>4 &period; To translate a string between two languages create a <strong><span style&equals;"color&colon; &num;008000&semi;">Translator<&sol;span><&sol;strong> object&comma; configure it with the source and target languages&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>val options &equals; TranslatorOptions&period;Builder&lpar;&rpar;<br &sol;> &period;setSourceLanguage&lpar;TranslateLanguage&period;ENGLISH&rpar;<br &sol;> &period;setTargetLanguage&lpar;TranslateLanguage&period;HINDI&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;>val englishHindiTranslator &equals; Translation&period;getClient&lpar;options&rpar;<&sol;pre> &NewLine;<p>5 &period; Next&comma; we need to ensure that the model has been downloaded before running the <strong><span style&equals;"color&colon; &num;008000&semi;">translate&lpar;&rpar;<&sol;span><&sol;strong> method&period; <&sol;p> &NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Downloads the model files required for translation&comma; if they are not already present<&sol;strong><&sol;span><br &sol;>englishHindiTranslator&period;downloadModelIfNeeded&lpar;&rpar;<br &sol;> &period;addOnSuccessListener &lbrace;<br &sol;> Log&period;e&lpar;TAG&comma; "Download Successful"&rpar;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace;<br &sol;> Log&period;e&lpar;TAG&comma; "Download Error&colon; " &plus; it&period;printStackTrace&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;<br &sol;> &rcub;<&sol;pre> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong><&sol;span> Avoid keeping too many language models &lpar;Its around 30MB&rpar; on the device at once &lpar;delete unneeded models&rpar; &comma;to prevent filling device memory&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<p>5 &period; Once the model has downloaded successfully&comma; pass a string of text in the source language to<span style&equals;"color&colon; &num;008000&semi;"><strong> translate&lpar;&rpar;&period;<&sol;strong><&sol;span><&sol;p> &NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Translates the given input from the source language into the target language&period;<&sol;span><&sol;strong><br &sol;>englishHindiTranslator&period;translate&lpar;edtSourceLangText&period;text&period;toString&lpar;&rpar;&rpar;<br &sol;> &period;addOnSuccessListener &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;set translated text in textview<&sol;strong><&sol;span><br &sol;> tvTargetLangText&period;setText&lpar;it&rpar;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Error<&sol;strong><&sol;span><br &sol;> Log&period;e&lpar;TAG&comma; "Error&colon; " &plus; it&period;localizedMessage&period;toString&lpar;&rpar;&rpar;<br &sol;> &rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>The translated text&comma; in the target language you configured&comma; is passed to the <span style&equals;"color&colon; &num;008000&semi;"><strong>success<&sol;strong><&sol;span> listener&period;<&sol;li> &NewLine;<li>In case of error&comma; exception is passed to the <strong><span style&equals;"color&colon; &num;008000&semi;">failure<&sol;span><&sol;strong> listener&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>6 &period; Ensure that the <span style&equals;"color&colon; &num;008000&semi;"><strong>close&lpar;&rpar;<&sol;strong><&sol;span> method is called when the Translator object will no longer be used&period; To do this on the Fragment or AppCompatActivity&comma; we need to write the below code&period;<&sol;p> &NewLine;<pre>val englishHindiTranslator &equals; Translation&period;getClient&lpar;options&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>getLifecycle&lpar;&rpar;&period;addObserver&lpar;englishHindiTranslator&rpar;<&sol;strong><&sol;span><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Complete MainActivity code<&sol;strong><&sol;span><&sol;h5> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> private val TAG &equals; MainActivity&colon;&colon;class&period;java&period;simpleName<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;<br &sol;><br &sol;> val edtSourceLangText&colon; EditText &equals; findViewById&lpar;R&period;id&period;edt&lowbar;source&lowbar;lang&lowbar;text&rpar;<br &sol;> val tvTargetLangText&colon; TextView &equals; findViewById&lpar;R&period;id&period;tv&lowbar;target&lowbar;lang&lowbar;text&rpar;<br &sol;> val btnTranslate&colon; Button &equals; findViewById&lpar;R&period;id&period;btn&lowbar;translate&rpar;<br &sol;> val progressBar&colon; ProgressBar &equals; findViewById&lpar;R&period;id&period;progressBar&rpar;<br &sol;><br &sol;> val options &equals; TranslatorOptions&period;Builder&lpar;&rpar;<br &sol;> &period;setSourceLanguage&lpar;TranslateLanguage&period;ENGLISH&rpar;<br &sol;> &period;setTargetLanguage&lpar;TranslateLanguage&period;HINDI&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;> val englishHindiTranslator &equals; Translation&period;getClient&lpar;options&rpar;<br &sol;> getLifecycle&lpar;&rpar;&period;addObserver&lpar;englishHindiTranslator&rpar;<br &sol;><br &sol;> btnTranslate&period;setOnClickListener&lpar;&lbrace;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;make sure that our EditText is not empty<&sol;strong><&sol;span><br &sol;> if &lpar;&excl;edtSourceLangText&period;text&period;isEmpty&lpar;&rpar;&rpar; &lbrace;<br &sol;><br &sol;> progressBar&period;visibility &equals; View&period;VISIBLE<br &sol;> btnTranslate&period;visibility &equals; View&period;GONE<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Downloads the model files required for translation&comma; if they are not already present englishHindiTranslator&period;downloadModelIfNeeded&lpar;&rpar;<&sol;span><&sol;strong><br &sol;> &period;addOnSuccessListener &lbrace;<br &sol;> Log&period;e&lpar;TAG&comma; "Download Successful"&rpar;<br &sol;> progressBar&period;visibility &equals; View&period;GONE<br &sol;> btnTranslate&period;visibility &equals; View&period;VISIBLE<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Translates the given input from the source language into the target language&period;<&sol;strong><&sol;span><br &sol;> englishHindiTranslator&period;translate&lpar;edtSourceLangText&period;text&period;toString&lpar;&rpar;&rpar;<br &sol;> &period;addOnSuccessListener &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Translation successful<&sol;strong><&sol;span><br &sol;> tvTargetLangText&period;setText&lpar;it&rpar;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Error<&sol;span><&sol;strong><br &sol;> Log&period;e&lpar;TAG&comma; "Error&colon; " &plus; it&period;localizedMessage&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace;<br &sol;> Log&period;e&lpar;TAG&comma; "Download Error&colon; " &plus; it&period;localizedMessage&rpar;<br &sol;> progressBar&period;visibility &equals; View&period;GONE<br &sol;> btnTranslate&period;visibility &equals; View&period;VISIBLE<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> &rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Run the App<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2938" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;11&sol;Screenshot&lowbar;20211110-221309&lowbar;TranslateTextExp1-498x1024&period;jpg" alt&equals;"" width&equals;"336" height&equals;"691" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;&NewLine;

Exit mobile version