The application language doesn't change completely - android

In application the language doesn't change completely. I select English. It say English. But when I go to another activity. The language change to primitive language. After I go to the main activity. The language is primative language. But when I click English or French(again) not any problem at all.
I think I didn't tell well. I hope you can understand well with video :
https://drive.google.com/file/d/1-5QL_5kdzg8d7QDe8UF8cwVii_-1S4JR/view?usp=sharing
The code for changing language :
public void dilidegistir(String dil){
Resources activityRes = getResources();
Configuration activityConf = activityRes.getConfiguration();
Locale newLocale = new Locale(dil);
activityConf.setLocale(newLocale);
activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics());
Resources applicationRes = getApplicationContext().getResources();
Configuration applicationConf = applicationRes.getConfiguration();
applicationConf.setLocale(newLocale);
applicationRes.updateConfiguration(applicationConf,
applicationRes.getDisplayMetrics());
finish();
startActivity(getIntent());
}
How can I solve this problem?
I need your help.

I don't know why you're trying to change the language at runtime (normally it should be handled on the system level based on device's settings), but if you really need it check out this article: https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
I hope it's helpful.

Related

Android setting Nightmode changes resource language

This is an effect that is difficult to describe.
Our Android app supports two languages, however we don't use the system language but let the user set this in the settings.
Then, before attaching the BaseContext of the Application we set the locale configuration.
// in Application class
override fun attachBaseContext(base: Context) {
super.attachBaseContext(LocaleHelper.onAttach(base))
}
// the LocaleHelper
fun onAttach(context: Context): Context {
return setLocale(context, getPersistedLanguage(context), getPersistedCountry(context))
}
That way the attachBaseContext call gets a context that has the locale set to e.g. "de" instead of "en" - even if the device is in English.
This works great so far and depending on the settings all resources coming from the context are in that language. However, we now added another setting for the night-mode (i.e. giving the user the option to set the "theme" in either "normal" or "dark mode").
For that reason the idea was to set something like this
if (enableDarkMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
in the onCreate() of the Application (we also tried in the Activity).
However, doing this, suddenly the resources (at least some) are loaded with the device locale. The menu entries are in the device language. However, checking the Locale.getLanguage() gives me the configured language and dynamically called Strings (e.g. context.getString(R.string.xyz)) also show in the correctly configured language.
This leads to the assumption that the menu resources are somewhat (re)loaded (again) but don't respect the set Locale from the JVM.
Does anyone have any idea how to find that bug? What are we missing here? Are the menu resources loaded differently?
I just discovered a hacky solution but in case there is anyone having the same problem this might help a bit:
I added to the activity in the manifest
android:configChanges="uiMode"
telling the Application to "handle ui mode changes myself".
In that case the resources stay "untouched" but I'm not sure what other implications this change might have.
So please let me know if you have any further hints on what's going wrong when letting the system / app handle the night mode changes itself.
Maybe late on this, but that can help other.
I managed to make sure that the dark theme doesn't change the locale language.
For that, I have a fragment which enable thank to a switch the dark theme.
SettingsFragment :
switchPreference.setOnPreferenceChangeListener((preference, isSwitchOn) -> {
if((boolean) isSwitchOn){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return true;
});
Then in the parent activity, I just have to override attachBaseContext !
SettingActivity :
#Override
protected void attachBaseContext(Context newBase) {
// Get configuration and resources before onCreate method
Resources resources = newBase.getResources();
Configuration configuration = new Configuration(resources.getConfiguration());
configuration.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED;
Context context = newBase.createConfigurationContext(configuration);
// Set locale with configuration saved
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String langue = sharedPreferences.getString("langage_pref", "fr");
Locale locale = new Locale(langue);
Locale.setDefault(locale);
configuration.setLocale(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
super.attachBaseContext(newBase);
}
Hope that will help you ! :D

Set Locale from App - Back Button Issue

I am working on Language setting on my app. I was able to change the locale from my Main Activity through the use of
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
configuration.setLocale(new Locale("ar"));
resources.updateConfiguration(configuration,displayMetrics);
recreate();
Everything worked fine but I noticed that the back button did not change its direction to RTL:
This is my expected behaviour when I set the language to a RTL language:
Is this possible?
Changing the navigation bar's direction means that the app will change the Locale of the device. Such a thing is similar to setting the device language in the language setting of the device since a Locale defines the language and direction (rtl or ltr) of the views to support the language. Since you're only changing the Locale of your Context in the app, it will not affect the Context of the other apps on the device and the system apps/components (launcher, navigation bar, status bar, etc...). Hence the navigation bar will not change and stay unaffected by the change in your app.
Furthermore, to alter the device Locale or any other configuration (font, font size, display metrics, rotation, etc..), the app would need the SET_CONFIGURATION permission to do so. Although it is not stated in the docs, the SET_CONFIGURATION permission can only be given automatically to system apps like the language and display settings. I haven't seen if it can be requested from the user by a third party app but I believe that it cannot be as most of the times in the development of my apps, the permission was being granted using adb shell pm grant command in the command line. Usually I would do that during my tests since my apps also support other languages and would want to make sure that the app performs well in such cases, hence I'd grant it the permission in the command line and let it to change the locale of the device from within the tests.
Try to call this method when you set arabic language:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
//HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then
//change if it is english then don't
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
The direction of the navigation bar (and other parts of the OS, like the Settings), is not related to an app. A user can have a LTR OS with a RTL app. thus changing your app's locale and direction does not affect the underlying OS and the user has to change it manually.

WebView language error Android N

I am aware of this question asked before about the WebView being broken in Android N especially with localization. I have another problem which I cannot figure out how to fix.
As shown in pic, I have two languages set on my Pixel device running 7.1.1. Now, I'm trying to load the following url in a WebView in my app.
"https://accounts.google.com/ServiceLogin?<my-params>"
What I notice is that the page loads in Chinese instead of English which is my current Locale language. I tried opening the above link on my Chrome desktop (where I am signed in with the same account as my phone) and it loads in Chinese too! I went to Chrome's settings to find that they language preferences are saved to my account because they show up in too.
I did try setting locale before setContentView() in my app like this:
public static void setLocale(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
Context context = MyApplication.getInstance();
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
but in vain. If I put a log statement, it prints the correct Locale which is en-US. The WebView still loads the page in Chinese though. Any thoughts on how can I fix it?
The issue is affected by the webview in the Android N.
On the first launching of the webview, it resets the locale to default.
If you then rotate the phone - the locale gets correctly set back to the custom locale.
If you then launch the webview again - the locale stays correct.
To fix this, you have to switch back to your chosen language after the page was loaded.
You may also check on the Activity/Fragment life cycle when to switch language.
Sources:
https://issuetracker.google.com/issues/37113860
https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese
Activity is blinking after locale change in Android 4.1+

make multi language android application

I created multi language (English, Russian, Uzbek) app. I put 4 string resoureses in 4 folders (values, values-en, values-ru, values-uz) as docs. When I change app language updates resourses configuration in App Controller like below:
Settings.LANGUAGE = prefs.getString(User.LANG, Settings.RUSSIAN);
Locale locale = new Locale(Settings.LANGUAGE);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
After that App restarts by calling App controller's method like below:
public void reStart() {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
After them It works well almost all devises. But on Samsung Galaxy S6 (SM-G920F), it works as crazy. Some words are in english and others are in Uzbek and ets.
So, How to fix this error? isn't the concepts of "Supporting Different Languages" supported by (applicable to) all devices?
By the way, I have checked that all resources are given in corresponding languages (as shown in attached image):
From my observations, weird behaviour was affecting only Activity titles, and I found that I was setting translations of activity titles in Manifest file. Only these translations were misbehaving. All other dynamically set translations were working fine.
So, to fix the problem, I removed all activity labels from Manifest file, then set activity titles in onCreate method as below:
getSupportActionBar().setTitle(R.string.title_activity_followers);
Problem solved.

Issue with setting locale within app

In my app, i am asking for language, can be either English or German. The selected language and its iso code is saved into preferences. On the basis of selected language i need to change all the texts into corresponding language.
For this, i have created res/values and res/values-de; each folder containing a strings.xml file. Issues are:
1) I am opening camera as well as a screen using opengl. After navigating via both of them, the texts does not change completely into german(if was chosen). Some text values change into German, rest not even on the same page.
2) Even without going through camera and opengl screens, the results are not achieved 100% always but gives a better result always as compared to case 1.
My implementations:
1) in onResume() of splash screen, i am changing locale based on preferences with the help of config.locale().
2) in manifest file, each activity is set with activity:configChanges="locale".
3) in camera activity and opengl activity, onConfigurationChanged() is overridden in which i am again setting locale as per preferences.
please guide how to solve the locale issue.
Check Point 1:
Partial updation of resources. Make sure you have resources named correctly in all your languages.
Also, you can give this code a try. It is how i update locale of my app:
public void updateLocale(String language) {
Locale myLocale = new Locale(language);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
Let me know if it helps !
I believe you need to add something to your AndroidManifest file indicating locale change. Here is an example: http://android.programmerguru.com/android-localization-at-runtime/
I had a similar problem with my app. App has force language changing option between Turkish and English. If my device language is English and if I use the app in Turkish, I can easily convert the language with the code below:
public void setAppLanguage(String languageCode) {
String countryCode;
if (languageCode.equals("tr")){
countryCode = "TR";
}else{
countryCode = "US";
}
Locale locale = new Locale(languageCode, countryCode);
Locale.setDefault(locale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
loginPrefsEditor.putString("uLang",languageCode);
loginPrefsEditor.apply();
}
However, when I open the camera and return from it, app language changes back to default phone language. App kills my activity when I open the camera. After return from the camera, it refreshes everything. So in order to solve this problem, I put my force change language method in onResume() method of every activity.
By doing this I would be able to solve this issue.

Categories

Resources