【问题标题】:Translate fields programmatically according to a specific Locale in Android根据 Android 中的特定语言环境以编程方式翻译字段
【发布时间】:2019-09-18 20:07:32
【问题描述】:

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

以下代码是我的 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;
    }
}

我的问题是当我点击“是”时,所有字段都没有被翻译。那么,如何让我的应用程序工作,所有字段都将被翻译。

【问题讨论】:

  • 您确定在您的 values-fr 文件夹中的 strings.xml 文件中提供了所有必要的翻译吗?
  • 是的,我将所有必要的翻译都放在 values-fr 文件夹中的 strings.xml 文件中

标签: android localization locale


【解决方案1】:

在我的情况下,此代码有效,请尝试:

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

【讨论】:

  • 这段代码对我不起作用,请给我另一个解决方案
  • 你试过没有getBaseContext() 吗?当我发布它?
  • 你没有创建新配置?
  • 这是新方法:private void setLocale(String lang) { Locale locale = new Locale(lang); Locale.setDefault(locale);配置配置 = 新配置(); config.locale = 语言环境; getResources().updateConfiguration(config, getResources().getDisplayMetrics()); SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit(); editor.putString("My_Lang", lang); editor.apply(); }
  • 不像我展示的那样,尝试从资源获取配置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多