Reputation: 150
I am working on an android project and I localized my app in two languages, the problem is that it works fine when I run my app directly from the android studio but when I upload my project in google play store and download the app from play store its localization is not working in some version of Android.
This is my java code that sets the local language.
private static Context updateResources(Context context) {
Locale locale = new Locale("fa","AF");
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
This is my English layout file.
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="update">Update</string>
<string name="app_version_title">New version</string>
<string name="app_version_title_second">is now available</string>
<string name="logout_msg">Are you sure you want to logout?</string>
<string name="logout_title">Log Out?</string>
<string name="languages">Languages</string>
<string name="km">Km</string>
</resources>
This is my Persion layout file.
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="update">به روز رسانی </string>
<string name="app_version_title">نسخه جدید</string>
<string name="app_version_title_second">موجود است</string>
<string name="logout_msg">آیا مطمئین هستید که خارج میشوید؟ </string>
<string name="logout_title">خروج؟</string>
<string name="languages">زبانها</string>
<string name="km">کیلومتر</string>
</resources>
This is the structure of my Values folder.
values strings.xml values-fa-rAF strings.xml
Upvotes: 15
Views: 4625
Reputation: 890
If you are using app bundle to publish in the play store the android app bundle feature will split the bundle into APK(s) based on the languages for optimizations and smaller sized APK file. Read more here. So only the APK which matches the user's locale will be installed on the device. Therefore changing the language dynamically will not work because the installed APK will contain only the language which is same as user phone's locale.
To prevent the split based on the language you need to add the following in your app build.gradle
file like below.
android {
...
...
bundle {
language {
enableSplit = false
}
}
}
Upvotes: 50
Reputation: 6461
I experienced the exact same issue. There is nothing wrong with your code.
It gave me hard times but I finally figured it out.
When you download an app from Google play - and you are trying to set a locale that does not exist in the Language list that the user defines in the Settings - the app will use the default strings.xml
ask from someone who experiences this issue to add Persian language to the Language list in the settings - then he must Uninstall the app - and re-Install it again - the app will use your strings.xml values-fa-rAF.
When you think about it - it has some sense in it.
Anyway - if it is critical you can consider checking on runtime the texts and show a message to the user and instruct him to solve the problem.
Good luck
Upvotes: 3
Reputation: 362
I think when you gonna to Generate signed bundle or APK, you select "Android App Bundle", you must Select "APK", because you are using strings.xml localization, you can Read more here.
Upvotes: -5