将应用程序中的区域设置自动更改为英语



I在我的应用程序上集成了区域设置(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);
}

同样在应用程序类上我添加了应用程序语言的代码,下面是应用程序类上的代码

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

创建一个基本活动并用您的活动扩展它,然后在基本活动上添加语言更改

低于

@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,并将该类扩展到主屏幕,

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

}

最新更新