I am working on globalization of Android app. I have to provide options to choose different locales from within the app. I am using the following code in my activity (HomeActivity) where I provide option to change the locale
Configuration config = new Configuration();
config.locale = selectedLocale; // set accordingly
// eg. if Hindi then selectedLocale = new Locale("hi");
Locale.setDefault(selectedLocale); // has no effect
Resources res = getApplicationContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
This works fine as long as there are no configuration changes like screen rotation where the locale defaults to the android system level locale rather than the locale set by the code.
Locale.setDefault(selectedLocale);
One solution I can think of is to persist the user selected locale using SharedPreferences and in each activity's onCreate() method have the locale set to the persisted locale as the onCreate() gets called again and again for every configuration change. Is there any better way to do this so that I don't have to do it in every activity.
Basically what I want is that - Once I change/set to some locale in my HomeActivity I want all the activities within my app to use that locale itself irrespective of any configuration changes....unless and until it is changed to other locale from the app's HomeActivity that provides options to change locale.
Configuration config = new Configuration(); config. locale = selectedLocale; // set accordingly // eg. if Hindi then selectedLocale = new Locale("hi"); Locale. setDefault(selectedLocale); // has no effect Resources res = getApplicationContext().
Go to app > res > values > right-click > New > Value Resource File and name it as strings. Now, we have to choose qualifiers as Locale from the available list and select the language as Hindi from the drop-down list. Below is the picture of the steps to be performed. Now, In this resource file string.
In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.
Although the solution stated in this answer works in the general case, I found myself adding to my preference screen:
<activity android:name="com.example.UserPreferences"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
This is because when the application is in landscape mode and the preference screen in portrait mode, changing the locale and going back to the application might cause trouble. Setting both to be in landscape mode prevent this from happening.
You need to change the locale at the Application level, so that its effects are everywhere.
public class MyApplication extends Application
{
@Override
public void onCreate()
{
updateLanguage(this);
super.onCreate();
}
public static void updateLanguage(Context ctx)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String lang = prefs.getString("locale_override", "");
updateLanguage(ctx, lang);
}
public static void updateLanguage(Context ctx, String lang)
{
Configuration cfg = new Configuration();
if (!TextUtils.isEmpty(lang))
cfg.locale = new Locale(lang);
else
cfg.locale = Locale.getDefault();
ctx.getResources().updateConfiguration(cfg, null);
}
}
Then in your manifest you have to write:
<application android:name="com.example.MyApplication" ...>
...
</application>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With