如何解决服务通知文本使用系统语言
我的应用支持 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());
}
解决方法
您的服务的
onCreate 方法。 如果需要,进行必要的调整。
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration,resources.getDisplayMetrics());
onStartCommand
的优化代码Locale locale;
if (MainActivity.LANG_CURRENT.equals("en"))
locale = new Locale("en");
else
locale = new Locale("iw");
Locale.setDefault(locale);
Resources resources = this.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config,resources.getDisplayMetrics());
,
此解决方案仅在您有预定义的通知字符串时才有效。
喜欢String.format("Your order id %s has been processed!",orderId);
在您的服务中,在绑定通知元数据的同时,让服务访问当前选择的语言。根据语言将格式字符串传递给一个单独的方法,该方法返回翻译后的标题和消息
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。