Change language for the whole application inside a fragment?

Let's say this is the call stack:

Fragment2 (current position)

Fragment1

HostingActivity

The user has just navigated to fragment2 (which is the settings screen). He chooses a second language, and then navigates back to Fragment1. I want the app to show content from the second language as soon as he enters fragment1.

I've read that the best approach would be to let all the fragments derive from a base-fragment which configures the locale inside the OnResume() method.

public class BaseFragment : Fragment
{
    public override void OnResume()
    {
        string langCode = prefs.GetString("settings_language", "en_US");

        Resources res = Application.Context.Resources;
        // Change locale settings in the app.
        DisplayMetrics dm = res.DisplayMetrics;
        Configuration conf = res.Configuration;
        conf.SetLocale(new Locale(langCode.ToLower()));

        res.UpdateConfiguration(conf, dm);
        OnConfigurationChanged(conf);

        base.OnResume();
    }
}

I've attempted this, but without any luck. I didn't see any change whatsoever.
I'm open to hear any suggestions.

PS. To ensure that I didn't mess up any of the naming conventions required by Android in the values folder, I tried rebooting the emulator (with adb shell) using the language I wanted and it worked as expected.

Sign In or Register to comment.