如何将阿拉伯语言设置为语言环境 [英] How to set Arabic language as Locale

查看:70
本文介绍了如何将阿拉伯语言设置为语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究文本到语音的转换.为此,我从互联网上得到了例子.他们通过 setLanguage(Locale.US); 设置英语.因此,现在我正在尝试设置阿拉伯语而不是英语.但是,当我将语言更改为阿拉伯语时,我失败了.有人帮助我将语言更改为阿拉伯语

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

参考代码

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);
        }

    }
}

推荐答案

在最近的参考中,Locale 有适合 ISO 639-1/ISO 3166 的语言/国家代码信息-1 .

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

ISO 639-1 是两个字母的小写语言代码.阿拉伯语是用这种格式 ar 定义的.

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);

此外,如果您想收听阿拉伯语语音,则使用的 TextToSpeech 服务必须支持阿拉伯语.先检查一下.

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

注意:

Android语言环境参考: https://developer.android.com/reference/java/util/Locale.html

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

ISO 639-1代码参考: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

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

这篇关于如何将阿拉伯语言设置为语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆