将应用程序中的语言环境自动更改为英语

Changing Locale within the app automatically to english

我在我的应用程序中集成了语言环境(enar)。一旦我将应用程序更改为阿拉伯语(ar),然后关闭并打开我的应用程序,它显示布局方向显示为阿拉伯语,但字符串正在加载英语。

这是我在单击按钮时更改语言的代码,

public static void setLocale(final Context ctx, final String lang) {
    Log.d(TAG, "##Changing Language to: " + lang);
    AppSettings.getInstance(ctx).save(PrefKeys.language, lang);
    final Locale loc = new Locale(lang);
 
    Resources resources = ctx.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        configuration.setLocale(loc);
    else
        configuration.locale = loc;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
        ctx.createConfigurationContext(configuration);
     else
        resources.updateConfiguration(configuration, displayMetrics);
}

也在应用程序上 class 我添加了应用语言的代码,这是应用程序上的代码 Class

 @Override
public void onCreate() {
    super.onCreate();
    String lanuage = AppSettings.getInstance(getApplicationContext()).getLanguage();
    setLocale(new Locale(lanuage));
}


private void setLocale(Locale locale){
        Log.d(TAG,  "##Changing Language to: " + locale.getLanguage());
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            configuration.setLocale(locale);
        } else{
            configuration.locale=locale;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
            getApplicationContext().createConfigurationContext(configuration);
        } else {
            resources.updateConfiguration(configuration,displayMetrics);
        }
    }

创建一个基础 activity 并用您的活动扩展它然后在基础上添加语言更改怎么样 activity

如下所示

@SuppressLint("Registered")
open class BaseActivity : AppCompatActivity() {


companion object {
    var lang: String? = null
}


override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(localeManager!!.setLocale(base!!))

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // AndroidThreeTen.init(this)
  Utility.resetActivityTitle(this)
}

}

我创建了新的 BaseActivity.class 并将 class 扩展到主屏幕,

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();


    @Override
    protected void attachBaseContext(Context newBase) {
        String language = AppSettings.getInstance(newBase).getLanguage();
        super.attachBaseContext(LocaleHelper.wrap(newBase, language));
    }


    private static class LocaleHelper extends ContextWrapper {

        public LocaleHelper(Context base) {
            super(base);
        }

        public static ContextWrapper wrap(Context context, String language) {
            if (TextUtils.isEmpty(language.trim())) {
                return new LocaleHelper(context);
            }
            Configuration config = context.getResources().getConfiguration();
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(locale);
            } else {
                //noinspection deprecation
                config.locale = locale;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                config.setLayoutDirection(locale);
                context = context.createConfigurationContext(config);
            } else {
                //noinspection deprecation
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
            return new LocaleHelper(context);
        }

    } // LocaleHelper

}

这里是HomeActivity的代码修改,

public class HomeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_home;
    }


}