如何将阿拉伯语言设置为Locale

时间:2016-02-10 12:20:07

标签: android locale text-to-speech

我正在进行文字转换。为此,我从互联网上得到了榜样。在此,他们将英语设置为setLanguage(Locale.US);。所以,现在我想设置阿拉伯语而不是英语。但是当我将语言改为阿拉伯语时,我失败了。任何人都帮我改变阿拉伯语的语言

参考代码

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

    }
}

2 个答案:

答案 0 :(得分:5)

在最近的参考资料中,font-size: 16px包含适用于 ISO 639-1 / ISO 3166-1 的语言/国家/地区代码信息。

ISO 639-1 是两个字母的小写语言代码。阿拉伯语以此格式定义为 ar

所以,试试这段代码:

font-size: initial

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

注意:

Android区域设置参考:https://developer.android.com/reference/java/util/Locale.html

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

答案 1 :(得分:2)

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

参考:ref