根据Android中的特定区域设置以编程方式翻译字段

时间:2019-04-30 07:52:39

标签: android localization locale

我想创建一个Android翻译应用,最小api 15,最大api 28。 我创建了一个简单的界面,其中包含一些TextViews,EditTexts和Buttons。 例如,我想将所有字段的文本从英语翻译为法语。单击时,此界面包含一个按钮,出现自定义警报对话框,其中包含确认文本“是”或取消文本“取消”。我的目标是当我单击“是”时,应以编程方式执行对特定语言(在我的情况下为法语)的翻译。

以下代码是我的mainActivity:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "";
    @BindView(R.id.changeLanguageButton)
    Button changeLanguage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoadLocale();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        changeLanguage.setOnClickListener(v -> {
            //DIALOG ALERT
            new SweetAlertDialog(MainActivity.this)
                    .setTitleText(getString(R.string.app_name))
                    .setContentText(getString(R.string.dialog_default_title))
                    .setConfirmText(getString(R.string.confirm))
                    .setConfirmClickListener(sweetAlertDialog -> {
                        Log.i(TAG,"test");
                        setLocale("fr");
                        recreate();
                    })
                    .setCancelText(getString(R.string.cancel))
                    .setCancelClickListener(sweetAlertDialog -> sweetAlertDialog.cancel())
                    .show();

        });
    }

      private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Resources res = this.getResources();
    Configuration config = new Configuration();
    DisplayMetrics dm = res.getDisplayMetrics();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.locale = locale;
    } else {
        config.setLocale(locale);
    }
    res.updateConfiguration(config, dm);
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();

}

    public void LoadLocale() {
        SharedPreferences prefs = getSharedPreferences("Settings", MODE_PRIVATE);
        String languge = prefs.getString("My_lang", "");
        setLocale(languge);
    }
}

“ SweetAlertDialog”是包含自定义Dialog的类。如下所示:

public class SweetAlertDialog extends Dialog implements View.OnClickListener {
    private String mTitleText;
    private String mContentText;
    private String mCancelText;
    private String mConfirmText;
    private OnSweetClickListener mCancelClickListener;
    private OnSweetClickListener mConfirmClickListener;
    @BindView(R.id.title_text)
    TextView titleTextView;
    @BindView(R.id.content_text)
    TextView contentTextView;
    @BindView(R.id.confirmbutton)
    Button confirmButton;
    @BindView(R.id.cancelbutton)
    Button cancelButton;

    public SweetAlertDialog(Context context) {
        super(context);
    }

    public interface OnSweetClickListener {
        void onClick(SweetAlertDialog sweetAlertDialog);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);
        ButterKnife.bind(this);
        confirmButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
        setTitleText(mTitleText);
        setContentText(mContentText);
        setCancelText(mCancelText);
        setConfirmText(mConfirmText);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cancelbutton) {
            if (mCancelClickListener != null) {
                mCancelClickListener.onClick(SweetAlertDialog.this);
            }else {
                dismiss();
            }
        } else if (v.getId() == R.id.confirmbutton) {
            if (mConfirmClickListener != null) {
                mConfirmClickListener.onClick(SweetAlertDialog.this);
            }else {
                dismiss();
            }
        }
    }

    @Override
    public void cancel() {
        dismiss();
    }

    public SweetAlertDialog setCancelClickListener (OnSweetClickListener listener) {
        mCancelClickListener = listener;
        return this;
    }

    public SweetAlertDialog setConfirmClickListener (OnSweetClickListener listener) {
        mConfirmClickListener = listener;
        return this;
    }

    public SweetAlertDialog setTitleText(String text) {
        mTitleText = text;
        if (titleTextView != null && mTitleText != null) {
            titleTextView.setText(mTitleText);
        }
        return this;
    }

    public SweetAlertDialog setContentText(String text) {
        mContentText = text;
        if (contentTextView != null && mContentText != null) {
            contentTextView.setText(mContentText);
        }
        return this;
    }

    public SweetAlertDialog setConfirmText(String text) {
        mConfirmText = text;
        if (confirmButton != null && mConfirmText != null) {
            confirmButton.setText(mConfirmText);
        }
        return this;
    }

    public SweetAlertDialog setCancelText(String text) {
        mCancelText = text;
        if (mCancelText != null && cancelButton != null) {
            cancelButton.setText(mCancelText);
        }
        return this;
    }
}

我的问题是,当我单击“是”时,所有字段均未翻译。因此,如何使我的应用正常工作,所有字段都将被翻译。

1 个答案:

答案 0 :(得分:0)

就我而言,此代码有效,请尝试:

    private void changeApplicationLanguage(Locale locale) {
            Resources res = this.getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            // Set new language settings.
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 
                conf.local = local
            } else { 
                conf.setLocal(local)
            }
            res.updateConfiguration(conf, dm);
            // Restart activity with new language settings.
            this.recreate();
        }