Set Locale from App - Back Button Issue - android

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.

Related

Android language change and settings are completely broken inside app downloaded from Play Store

I have a function to rewrite and pick app language based on system language while you install an app. This function is working as expected if you build your app from Android Studio (even release build). Also, I can confirm it is working from Play Store on all Androids except Android 10, 11 and 12.
It looks like I will correctly pick locale according to my logs and I will rewrite resource config, but after activity restart, it will jump to English as default no matter what system language is currently set (even if default locale in code is correct - in my case Czech (lang code "cs").
As I said, it is caused by the Google Play version of APK, not from Android Studio one.
Is there any undocumented change by Google Play Terms of Service, that they are blocking resource config to be read-only if uploaded on Play Store since Android 10?
Here is function:
fun applyLanguage() {
val defaultLocale = startupLocale
val langs = App.languages
val langCode = app.languageIndex.let {
if (it == 0) {
if(langs.any{ l -> l.first==defaultLocale.language }) {
defaultLocale.language
}else {
langs[App.LANGUAGE_INIT_DEFAULT].first
}
} else {
langs[it - 1].first
}
}
App.log("LangChange: MainActivity -> applyLanguage (langCodeSet) $langCode")
app.sysLog("LangChange: MainActivity -> applyLanguage (langCodeSet) $langCode")
if (resources.configuration.locale.language != langCode) {
val l = if (langCode == defaultLocale.language) {
defaultLocale
} else
Locale(langCode, "", "")
arrayOf(resources, app.resources).forEach { res ->
val cfg = res.configuration
cfg.locale = l
res.updateConfiguration(cfg, res.displayMetrics)
}
Locale.setDefault(l)
}
app.langCode = if (langs.any { it.first == langCode }) langCode else "cs"
App.log("LangChange: MainActivity -> applyLanguage (langCode) ${app.langCode}")
app.sysLog("LangChange: MainActivity -> applyLanguage (langCode) ${app.langCode}")
}
Its simple function, I have an array of available languages (in my case it's 3 of them based on available resources - translates) and if the default system language is one of them I will set the app in that language, if it's not I will set Czech as default. So if I pick English, I should have the app in English, if I pick German, I should have the app in German, if I pick Czech, I should have the app in Czech and if I pick any other language (for example French) it should be set to Czech as a fallback is there.
Also, the same function is used for language picker in App settings and it's the same issue. Default locale has langCode "cs" but if I pick any of those languages from the picker, it will always set resources to default state (string.xml file) which is of course English.
Another example, I setup default language as French in device settings. I downloaded an app from store and it correctly rewrites resources locale to Czech (language code "cs"). But app was still in English.
So, resources.configuration.locale.language was "cs" after activity restart, but this resource config was completely ignored by the system and system picked default resource xml - string.xml which is English.
So it looks like you cant rewrite the resources config anymore, or technically you can, but this altered resource config is completely ignored by the system.
UPDATE
Additional debugging.
Android 10: Default language (French): App was installed and default language was set to English(suppose to be Czech).
If you change language in settings, no matter what language you pick, it will always be set to English.
Android 11: Default language (French): App was installed and default language was set to Czech(correct).
If you change your language in settings it gets interesting:
If you change to English, app switches to English. If you change back to Czech, app switches to Czech. If you change to German, app switches to English (I dont know whats going on).
Android 12: Default language (French): App was installed and default language was set to English(suppose to be Czech).
If you change to English, app switches to English. If you change back to Czech, app switches to English. If you change to German, app switches to German.
Android 9, 8, 7, 6 (and probably lower) - working as intended.
I'm not sure whats going on but its kinda funny.
According to your description above, I got that your issue only happened in the Google Play app version.
I had faced a similar issue, all I did is adding this setting:
bundle {
language {
enableSplit = false
}
}
build.gradle(.app) within android tag
To expand Eng. OsamaYousef answer.
In Short you have to disallow bundle to remove "not needed" languages when user downloads your application.
bundle {
language {
enableSplit = false
}
}
Explanation:
The reason your language picker is working with android studio and not with play store version is because through android studio you install .apk and play store installs .aab (bundle). To test what user gets by downloading your app through play store simply go to android studio -> Run -> Edit configurations... -> Under installation Options search for Deploy and select APK from app bundle.
In addition, I would also recommend following Android guide to implement latest recommendations.

Changing application direction on the fly without restarting the whole application

I'm developing a react native application in two languages, English and Persian(Farsi).
I'm using I18nManager for setting the application layout direction, but I manage translations by myself and I don't use I18nManager for translation. so please note that my application translation is not related to the system locale. and the application locale is completely independent from the system locale.
I pass application language as a prop to all children components in the hierarchy and manage their changes in App.js component (as the root component of system). (in App.js there is a state called applicationLanguage)
I have a function below that I am calling after setState for changing the applicationLanguage:
setApplicationDirection = (language: Language) => {
I18nManager.allowRTL(language === Language.Farsi);
I18nManager.forceRTL(language === Language.Farsi);
};
usage of setApplicationDirection:
onChangeLanguage = (language: Language) => {
this.setState({
applicationLanguage: language
});
this.setApplicationDirection(language);
};
after onChangeLanguage my translations has been changed without application restart. but application direction does not changed.
I know I can use react-native-restart and manually restart the application to take effect, but I would really prefer if changes would occur without restarting the application to provide a better UX. In my application the user can change the language in every screen and if I restart the application, he has to route to that screen again.

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+

Android - WebView language changes abruptly on Android 7.0 and above

I have a multilingual app with primary language English and secondary language Arabic.
As described in the documentation,
I have added android:supportsRtl="true" in the manifest.
I have changed all xml properties with left and right attributes to start and end respectively.
I have added Arabic language strings in strings-ar (and similarly for other resources).
The above setup works properly. After changing the Locale to ar-AE, Arabic text & resources are correctly displayed in my Activities.
However, every time I navigate to an Activity with a WebView
and/or a WebViewClient, the locale, text and layout direction
abruptly revert to the device default.
Further hints:
This is occurring only on a Nexus 6P with Android 7.0. Everything works properly on Android 6.0.1 and below.
The abrupt shift in locale happens only when I navigate to an Activity that has a WebView and/or a WebViewClient (and I have several). It does not occur on any of the other Activities.
Android 7.0 has multi-locale support, allowing the user to set more than one default locale. So if I set the primary locale to Locale.UK:
Then on navigating to the WebView, the locale changes from ar-AE
to en-GB.
Android 7.0 API changes:
As indicated in the list of API changes, new methods pertaining to locale have been added to the following classes in API 24:
Locale:
Locale.getDefault(...)
Locale.setDefault(...)
Configuration:
getLocales()
setLocales(...)
However, I am building my app with API 23, and am not using any of
these new methods.
Furthermore ...
The problem occurs on the Nexus 6P emulator as well.
To get the default locale, I am using Locale.getDefault().
To set the default locale, I am using the following code:
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());
}
Has anyone encountered this problem before? What is the reason for it, and how do I resolve this?
References:
1. Native RTL support in Android 4.2.
2. Multilingual Support - Language and Locale.
3. Be wary of the default locale.
Ted Hopp's answer managed to solve the problem, but he didn't address the question of why this occurs.
The reason is the changes made to the WebView class and its support package in Android 7.0.
Background:
Android's WebView is built using WebKit. While it was originally a part of AOSP, from KitKat onwards a decision was made to spin off WebView into a separate component called Android System WebView. It is essentially an Android system app that comes pre-installed with Android devices. It is periodically updated, just like other system apps such as Google Play Services and the Play Store app. You can see it in your list of installed system apps:
Android 7.0 changes:
Starting with Android N, the Chrome app will be used to render any/all WebViews in third-party Android apps. In phones that have Android N out-of-the-box, the Android WebView System app is not present at all. In devices that have received an OTA update to Android N, the Android System WebView is disabled:
and
Moreover, multi-locale support has been introduced, with devices having more than one default language:
This has an important consequence for apps that have multiple languages. If your app has WebViews, then those are rendered using the Chrome app. Because Chrome is an Android app in itself, running in its own sandboxed process, it will not be bound to the locale set by your app. Instead, Chrome will revert to the primary device locale. For example, say your app locale is set to ar-AE, while the primary locale of the device is en-US. In this case, the locale of the Activity containing a WebView will change from ar-AE to en-US, and strings and resources from the corresponding locale folders will be displayed. You may see a mish-mash of LTR and RTL strings/resources on those Activitys that have WebViews.
The Solution:
The complete solution to this problem consists of two steps:
STEP 1:
First, reset the default locale manually in every Activity, or at least every Activity that has a WebView.
public static void setLocale(Locale locale){
Context context = MyApplication.getInstance();
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale.setDefault(locale);
configuration.setLocale(locale);
if (Build.VERSION.SDK_INT >= 25) {
context = context.getApplicationContext().createConfigurationContext(configuration);
context = context.createConfigurationContext(configuration);
}
context.getResources().updateConfiguration(configuration,
resources.getDisplayMetrics());
}
Call the above method before calling setContentView(...) in the onCreate() method of all your Activities. The locale parameter should be the default Locale that you wish to set. For example, if you wish to set Arabic/UAE as the default locale, you should pass new Locale("ar", "AE"). Or if you wish to set the default locale (i.e. the Locale that is automatically set by the operating system), you should pass Locale.US.
STEP 2:
Additionally, you need to add the following line of code:
new WebView(this).destroy();
in the onCreate() of your Application class (if you have one), and wherever else the user may be changing the language. This will take care of all kinds of edge cases that may occur on app restart after changing the language (you may have noticed strings in other languages or with the opposite alignment after changing the language on Activities that have WebViews on Android 7.0++).
As an addendum, Chrome custom tabs are now the preferred way of rendering in-app web pages.
References:
1. Android 7.0 - changes for WebView.
2. Understanding WebView and Android security patches.
3. WebView for Android.
4. WebView: From "Powered by Chrome" to straight up Chrome.
5. Nougat WebView.
6. Android 7.0 Nougat.
7. Android N Mysteries, Part 1: Android System WebView is just "Chrome" Now?.
Your code seems to be setting the locale in the configuration for the app itself (MyApplication.getInstance()). However, you need to update the configuration for the activity context before inflating the activity's content view. I've found that modifying the app's context isn't enough (and, as it turns out, isn't even necessary). If I don't update each activity context, then the behavior is inconsistent across activities.
The way I approach this is to subclass AppCompatActivity (or Activity, if not using the compatibility library) and then derive all my activity classes from that subclass. Here's a simplified version of my code:
public class LocaleSensitiveActivity extends AppCompatActivity {
#Override protected void onCreate(Bundle savedInstanceState) {
Locale locale = ... // the locale to use for this activity
fixupLocale(this, locale);
super.onCreate(savedInstanceState);
...
}
static void fixupLocale(Context ctx, Locale newLocale) {
final Resources res = ctx.getResources();
final Configuration config = res.getConfiguration();
final Locale curLocale = getLocale(config);
if (!curLocale.equals(newLocale)) {
Locale.setDefault(newLocale);
final Configuration conf = new Configuration(config);
conf.setLocale(newLocale);
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
private static Locale getLocale(Configuration config) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return config.getLocales().get(0);
} else {
//noinspection deprecation
return config.locale;
}
}
}
Then I make sure to call super.onCreate(savedInstanceState) in each subclass's onCreate() method before calling any methods (such as setContentView()) that use the context.
This has an important consequence for apps that have multiple languages. If your app has WebViews, then those are rendered using the Chrome app. Because Chrome is an Android app in itself, running in its own sandboxed process, it will not be bound to the locale set by your app. Instead, Chrome will revert to the primary device locale. For example, say your app locale is set to ar-AE, while the primary locale of the device is en-US. In this case, the locale of the Activity containing a WebView will change from ar-AE to en-US, and strings and resources from the corresponding locale folders will be displayed. You may see a mish-mash of LTR and RTL strings/resources on those Activitys that have WebViews.
The Solution:
The complete solution to this problem consists of two steps:
STEP 1:
First, reset the default locale manually in every Activity, or at least every Activity that has a WebView.
public static void setLocale(Locale locale){
Context context = MyApplication.getInstance();
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale.setDefault(locale);
configuration.setLocale(locale);
if (Build.VERSION.SDK_INT >= 25) {
context = context.getApplicationContext().createConfigurationContext(configuration);
context = context.createConfigurationContext(configuration);
}
context.getResources().updateConfiguration(configuration,
resources.getDisplayMetrics());
}
Call the above method before calling setContentView(...) in the onCreate() method of all your Activities. The locale parameter should be the default Locale that you wish to set. For example, if you wish to set Arabic/UAE as the default locale, you should pass new Locale("ar", "AE"). Or if you wish to set the default locale (i.e. the Locale that is automatically set by the operating system), you should pass Locale.US.
STEP 2:
Additionally, you need to add the following line of code:
new WebView(this).destroy();
in the onCreate() of your Application class (if you have one), and wherever else the user may be changing the language. This will take care of all kinds of edge cases that may occur on app restart after changing the language (you may have noticed strings in other languages or with the opposite alignment after changing the language on Activities that have WebViews on Android 7.0++).
As an addendum, Chrome custom tabs are now the preferred way of rendering in-app web pages.
After reading all answers I found out that there is something missing in each one so here is the solution that worked for me so far. Since the WebView overrides the language configuration of the activity's context and application context, you must make sure each time this happens you call a method that resets those changes back. In my case I wrote following class that my activities which present this problem extend (those showing a WebView):
public class WebViewFixAppCompatActivity extends AppCompatActivity {
private Locale mBackedUpLocale = null;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mBackedUpLocale = getApplicationContext().getResources().getConfiguration().getLocales().get(0);
}
}
#Override
protected void onStop() {
super.onStop();
fixLocale();
}
#Override
public void onBackPressed() {
fixLocale();
super.onBackPressed();
}
/**
* The locale configuration of the activity context and the global application context gets overridden with the first language the app supports.
*/
public void fixLocale() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Resources resources = getResources();
final Configuration config = resources.getConfiguration();
if (null != mBackedUpLocale && !config.getLocales().get(0).equals(mBackedUpLocale)) {
Locale.setDefault(mBackedUpLocale);
final Configuration newConfig = new Configuration(config);
newConfig.setLocale(new Locale(mBackedUpLocale.getLanguage(), mBackedUpLocale.getCountry()));
resources.updateConfiguration(newConfig, null);
}
// Also this must be overridden, otherwise for example when opening a dialog the title could have one language and the content other, because
// different contexts are used to get the resources.
Resources appResources = getApplicationContext().getResources();
final Configuration appConfig = appResources.getConfiguration();
if (null != mBackedUpLocale && !appConfig.getLocales().get(0).equals(mBackedUpLocale)) {
Locale.setDefault(mBackedUpLocale);
final Configuration newConfig = new Configuration(appConfig);
newConfig.setLocale(new Locale(mBackedUpLocale.getLanguage(), mBackedUpLocale.getCountry()));
appResources.updateConfiguration(newConfig, null);
}
}
}
}
The idea posted by #Tobliug to save the initial configuration before the WebView overrides it worked for me, in my particular case I found this to be more easy to implement than other solutions posted.
Important is that the fix method gets called after exiting the WebView, e.g. when pressing back and in onStop.
If the webView is shown in a dialog you must take care the fix method is called after dismissing the dialog, mostly in onResume and/or onCreate. And if the webView is directly loaded in onCreate of the Activity and not afterwards in a new fragment the fix must also be called directly after setContentView before the activity's title is set, etc. If the WebView is loaded inside a fragment in the activity, call the activity in onViewCreated of the fragment and the activity should call the fix method.
Not all activities need to extend the class above as noted in an aswer, that's an overkill and not necessary.
This issue also does not get solved replacing the WebView by Google Chrome Tabs or opening an external browser.
If you really need your ressources configuratoin to have the whole list of languages set and not only one, then you would need to merge this solution with the one at https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce
In my case it was not necessary.
I also did not find necessary to call new WebView(this).destroy(); as noted in an answer here.
Same issue here. I have a dirty, but simple, solution.
Because I observe that the locale is still good in the Activity.onCreate(...) function and no more valid in the Activity.onPostCreate(...) function, I just save the Locale and force it at the end of the onPostCreate(...) function.
Here we go :
private Locale backedUpLocale = null;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
backedUpLocale = getApplicationContext().getResources().getConfiguration().locale;
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
changeLocale(backedUpLocale);
}
Bonus - the change locale function :
public void changeLocale(final Locale locale) {
final Configuration config = res.getConfiguration();
if(null != locale && !config.locale.equals(locale)) {
Locale.setDefault(locale);
final Configuration newConfig = new Configuration(config);
if(PlatformVersion.isAtLeastJellyBeanMR1()) {
newConfig.setLocale(new Locale(locale.getLanguage()));
} else {
newConfig.locale = new Locale(locale.getLanguage());
}
res.updateConfiguration(newConfig, null);
}
}
Hopes it will help.
I want to add one more use-case here:
When pressing back from webview activity(i.e. Showing payment screen and user press back button), onCreate() of previous activity does not execute, So that language got reset again. To keep it bug free, We must reset app locale in onResume() of base Activity.
private static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
config.setLayoutDirection(locale);
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
Call above method in onResume() of base activity or atleast in webview activity.
Edit:
If you are dealing with Fragments, make sure this method called when user exit from webview.
I have noticed that this happens only the first time you use a web view in the app , but it will not happen after that (i.e if the language is changed from inside the app and you opened a web view -again- or another webview ,then this time the webview will not change back the language as it did in the first time.
so based on that, a simple solution that I have done is I have added a webview with visibility gone to my very first activity in the app(or the host activity if you use single activity app with fragments) then I apply my locale in that activity's onCreate method after the call to setContentView.So in my activity.xml I will have :
<WebView
android:id="#+id/webview"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
and in my activity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)//at this moment and because there is the webview in the xml, the language will be reverted to the device's language
updateLocal(this ,"en")//this reset it back to whatever language you want to use(from shared preferences for example )
}
and updateLocal having code like following :
fun updateLocale(
c: Context,
languageToSwitchTo: String
): Context {
val locale = Locale(languageToSwitchTo)
Locale.setDefault(locale)
var context = c
val resources = context.resources
val configuration: Configuration = resources.configuration
configuration.setLayoutDirection(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val localeList = LocaleList(locale)
LocaleList.setDefault(localeList)
configuration.setLocales(localeList)
} else {
configuration.locale = locale
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
context = context.createConfigurationContext(configuration)
}
resources.updateConfiguration(configuration, resources.displayMetrics)
return context
}
In Android N, when you do new WebView(), it will add /system/app/WebViewGoogle/WebViewGoogle.apk to resource path. And if it was not added to the path already, it will cause Resource recreate (only the first time you use WebView in the app).
So if you want to solve the problem, just do new WebView(applicationContext) in Application#OnCreate() before you change the Locale.
If you know Chinese, you can read this blog.
None of the answers above helped me, I managed to reset app locale again inside onStop() method of the activity containing the Webview
If you are using the WebView only to display rich text (Text with some paragraphs or bold and italic text in different font sizes), then you can use TextView and Html.fromHtml() instead. TextViews have no issue with locale settings ;-)
Just Change Parameter For SEt Local Method From Passing BaseContext To "this" Or Exact activity Specially On android 7.0 and older
I'm facing the same issue in the fragment.
Resolving this by setting language again in the onDestroyView function
#Override
public void onDestroyView() {
Utils.setLanguage(requireActivity());
super.onDestroyView();
}
I fixed the problem of pronunciation by changing the language of HTML using the lang tag.
HTML Language Accessibility

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.

Categories

Resources