0

我的应用程序支持 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());
    
}
4

2 回答 2

2

您的服务的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());
于 2021-04-02T13:12:17.273 回答
0

此解决方案仅在您具有预定义的通知字符串时才有效。 喜欢String.format("Your order id %s has been processed!",orderId);

在您的服务中,在绑定通知元数据时,让服务访问当前选择的语言。根据语言将格式字符串传递给一个单独的方法,该方法返回翻译后的标题和消息

于 2021-04-02T12:47:03.910 回答