我的应用程序支持 2 种语言,英语和希伯来语。当我将语言更改为英语并最小化应用程序时,服务通知文本以希伯来语而不是英语显示。除了通知之外,翻译工作完美。
现在正如我在标题中提到的那样,我发现它的发生是因为我的系统语言是希伯来语。
我该如何克服这个问题?
谢谢 !
编辑:
更改语言按钮(仅 MainActivity,来自弹出菜单) -
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem menu_item) {
int itemId = menu_item.getItemId();
if (itemId == R.id.English) {
changeLang(getApplicationContext(), "en");
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else if (itemId == R.id.Hebrew) {
changeLang(getApplicationContext(), "iw");
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
return true;
}
});
@Override
protected void attachBaseContext(Context newBase) {
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(newBase);
LANG_CURRENT = preferences.getString("Language", "en");
super.attachBaseContext(MyContextWrapper.wrap(newBase, LANG_CURRENT));
}
public void changeLang(Context context, String lang) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Language", lang);
editor.apply();
}
服务 onCreate -
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (MainActivity.LANG_CURRENT.equals("en")) {
changeLang(getApplicationContext(),"en");
Lang = "en";
} else {
changeLang(getApplicationContext(),"iw");
Lang = "iw";
}
}
服务通知 -
NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch), activityPendingIntent);
NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.stop), servicePendingIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.launch))
.addAction(action)
.addAction(action2)
.setColor(getColor(R.color.foreGroundBackgroundColor))
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.applogo)
.build();
startForeground(2, notification);
除了服务通知外,翻译对所有活动都非常有效。
感谢@Eyosiyas的解决方案 -
服务活动 -
在 onStartCommand 我这样做了 -
if (MainActivity.LANG_CURRENT.equals("en")) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Resources resources = this.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
} else {
Locale locale = new Locale("iw");
Locale.setDefault(locale);
Resources resources = this.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
}