Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Arabic language as Locale

I'm working on text to speech conversion. For this, I got the example from the internet. In this they set the English language by setLanguage(Locale.US);. So, now I'm trying to set the Arabic instead of English. But I failed when I change the language to Arabic. Anyone help me to change the language as Arabic

Code for reference

import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TexttoSpeechActivity extends Activity implements OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {
        // TODO Auto-generated method stub

        if (status == TextToSpeech.SUCCESS) {

            /*Locale locale = new Locale("ar_EG");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                  getBaseContext().getResources().getDisplayMetrics());*/

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);

            }

        } else {
            Log.e("TTS", "Initilization Failed");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();
        if (text.length() == 0) {
            tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
        } else {
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }

    }
}
like image 795
Bahu Avatar asked Jan 06 '23 16:01

Bahu


2 Answers

In recent reference, Locale has information of language/country code which are suitable in ISO 639-1/ISO 3166-1.

ISO 639-1 is two-letter lowercase language code. Arabic is defined ar in this format.

So, try this code:

Locale loc = new Locale("ar");
/*  under API 20 */
tts.setLanguage(loc);

/* over API 21 */
String voiceName = loc.toLanguageTag();
Voice voice = new Voice(voiceName, loc, Voice.QUALITY_HIGH, Voice.LATENCY_HIGH, false, null);
tts.setVoice(voice);

Also, TextToSpeech service that you used must support Arabic if you want to listen Arabic voice. Check it first.

Note:

Android Locale reference: https://developer.android.com/reference/java/util/Locale.html

ISO 639-1 code reference: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

like image 158
Kae10 Avatar answered Jan 17 '23 00:01

Kae10


Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

Reference :ref

like image 31
Hussnain Azam Avatar answered Jan 16 '23 23:01

Hussnain Azam