在 API 22 字符串不更改的设备中更改应用程序语言
Change App Language in devices with API 22 String don't change
我想在更改应用语言环境时更改字符串
有个人资料片段
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.changeLanguageButton:
if (PrefManager.getLocale(getActivity()).equals("en")) {
setLocale("ar");
} else {
setLocale("en");
}
break;
}
}
public void setLocale(String lang ) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(getActivity(), lang);
Intent intent = new Intent(getActivity(), SplashActivity.class);
startActivity(intent);
}
有飞溅Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (PrefManager.getLocale(this) == null){
setLocale("en");
}else if (PrefManager.getLocale(this).equals("en")){
setLocale("en");
}else {
setLocale("ar");
}
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, 3000);
}
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
this.getBaseContext().getResources().updateConfiguration(config, this.getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(this, lang);
}
问题是此代码在 API 23 的设备中工作并翻译所有字符串但在 API 22 的设备中不起作用它只是改变布局方向
我的 minSDKVersion 19 & targetSDKVersion 29
如果有人感兴趣,我找到了答案
我将阿拉伯字符串文件名从“string (ar-rEG)”更改为“string (ar)”
而且效果很好
我想在更改应用语言环境时更改字符串
有个人资料片段
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.changeLanguageButton:
if (PrefManager.getLocale(getActivity()).equals("en")) {
setLocale("ar");
} else {
setLocale("en");
}
break;
}
}
public void setLocale(String lang ) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config, getActivity().getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(getActivity(), lang);
Intent intent = new Intent(getActivity(), SplashActivity.class);
startActivity(intent);
}
有飞溅Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (PrefManager.getLocale(this) == null){
setLocale("en");
}else if (PrefManager.getLocale(this).equals("en")){
setLocale("en");
}else {
setLocale("ar");
}
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, 3000);
}
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(locale);
this.getBaseContext().getResources().updateConfiguration(config, this.getApplication().getResources().getDisplayMetrics());
PrefManager.saveLocale(this, lang);
}
问题是此代码在 API 23 的设备中工作并翻译所有字符串但在 API 22 的设备中不起作用它只是改变布局方向
我的 minSDKVersion 19 & targetSDKVersion 29
如果有人感兴趣,我找到了答案
我将阿拉伯字符串文件名从“string (ar-rEG)”更改为“string (ar)”
而且效果很好