My app support 2 languages,English and Hebrew. When I change the language to English and minimize the app, the service notification text showing in Hebrew instead of English.
Besides of the notification,the translation is working perfectly.
Now as I mentioned in the title,I figured out that its happening because my system language is Hebrew.
How can I overcome this issue?
Thank you!
EDIT:
Change Language Button (Only MainActivity,From popup menu) -
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();
}
Service 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";
}
}
Service Notification -
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);
The translation is working perfectly for all the activities except for the service notification..
THE SOLUTION THANKS TO @Eyosiyas -
Service Activity -
In onStartCommand I did this -
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 method of your service. Make the necessary adjustments if required.
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
Optimized code for 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());