Open mukswilly opened 4 years ago
Hi @napsterphantom ,
Do you use context.createConfigurationContext(config)
somewhere in your app to create a localized context?
Could you please provide a sample project reproducing the issue since there are a couple of similar issues reported already, but I still can't identify the problem without seeing the code.
Thanks in advance!
@napsterphantom , I also have the same problem in my app while using this library. Language inconsistency. but it does not always happen.
This is from my Application onCreate() method.
Lingver.init(this, Constants.LOCALE_MYANMAR) val language = pref.getLocaleLanguage(Constants.KEY_LOCALE_LANGUAGE, Constants.LOCALE_MYANMAR) Lingver.getInstance().setLocale(this, language)
Setting.. when user changes the langauge.
pref.saveToSharedPref(Constants.KEY_LOCALE_LANGUAGE, list[selectedItem].first) Lingver.getInstance().setLocale(this, list[selectedItem].first) dialog.dismiss() val i = Intent(this, SplashScreenActivity::class.java) startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)) System.exit(0)
Thanks
I can consistently reproduce this issue if device language is different than the one chosen in the app and then I open any browser intent and come back to the app. Any possible workaround?
@aconsuegra could you clarify what is your browser intent? do you open a build-in WebView or intent for an external browser like Google Chrome?
there is a caveat while using web view, you can read more on that in the README.
@mirrorlarry I see an issue in your sample code. After setting a locale you immediately kill the app process by System.exit(0)
, potentially preventing shared preferences to successfully persist the setting since it's done asynchronously (on a background thread).
For most of the cases, just restarting the activity back stack (FLAG_ACTIVITY_CLEAR_TASK
/FLAG_ACTIVITY_NEW_TASK
) is enough to apply new locale, no need to restart a whole app process.
If you indeed need to restart a whole process, you can provide a custom implementation for LocaleStore
and persist the setting synchronously just before killing the app.
@YarikSOffice You are right, my issue is solved after applying the suggested fix
@napsterphantom , I also have the same problem in my app while using this library. Language inconsistency. but it does not always happen.
This is from my Application onCreate() method.
Lingver.init(this, Constants.LOCALE_MYANMAR) val language = pref.getLocaleLanguage(Constants.KEY_LOCALE_LANGUAGE, Constants.LOCALE_MYANMAR) Lingver.getInstance().setLocale(this, language)
Setting.. when user changes the langauge.
pref.saveToSharedPref(Constants.KEY_LOCALE_LANGUAGE, list[selectedItem].first) Lingver.getInstance().setLocale(this, list[selectedItem].first) dialog.dismiss() val i = Intent(this, SplashScreenActivity::class.java) startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)) System.exit(0)
Thanks@aconsuegra could you clarify what is your browser intent? do you open a build-in WebView or intent for an external browser like Google Chrome?
there is a caveat while using web view, you can read more on that in the README.
@mirrorlarry I see an issue in your sample code. After setting a locale you immediately kill the app process by
System.exit(0)
, potentially preventing shared preferences to successfully persist the setting since it's done asynchronously (on a background thread).For most of the cases, just restarting the activity back stack (
FLAG_ACTIVITY_CLEAR_TASK
/FLAG_ACTIVITY_NEW_TASK
) is enough to apply new locale, no need to restart a whole app process.If you indeed need to restart a whole process, you can provide a custom implementation for
LocaleStore
and persist the setting synchronously just before killing the app.
Thank you
In my case it was initialize Admob lately, instead of inside App#onCreate. I don't know what they doing in that method and causing bug.
Had similar problems after Webwiew was opened inside app. Some ui elements like button texts were not translated. Calling Lingver.getInstance().setLocale(...) in onCreateView of Fragment with Webwiew solved the problem
I also faced this issue. For my use case, I used Custom Tab Intent by androidx browser library to launch the URL.
components of the app that use strings directly in xml eg the layouts correctly respond to language changes, however, Strings pulled by getString(R.string.xxx) remain to the system locale language