Forum Xamarin Xamarin.Forms

Change language from Picker component

arturmullerarturmuller Member ✭✭✭

I followed this tutorial to add localization to my app: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text?pivots=windows

However, it seems like the example shown here only reads the culture selected in the OS (typically Android settings etc) and uses that. I have a column in my database for each user and their language. Whenever the user starts the app for the first time, I would like to use the device's default language.

But after that, in my settings page I have a Picker component where my user can select from a list of languages which would localize my app only and has nothing to do with the culture of the device.

How can I programmatically change the language/culture of my app when the user selects a new language from the Picker? Its not an issue if the user would need to restart the app. I tried setting CurrentCulture and some other parameters but the app keeps showing the default culture of the device. Target platforms are Android and iOS. I have seen a few examples online but mostly they are using the device default culture.

Answers

  • FabricioBertaniFabricioBertani USMember ✭✭✭

    You must do this natively on each platform and use Dependency Service, this peace of code will work for both platforms:

    public void SetApplicationLanguage(CultureInfo ci)
    {
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
        CultureInfo.DefaultThreadCurrentCulture = ci;
        CultureInfo.DefaultThreadCurrentUICulture = ci;
    }
    

    And when you use it, ensure to pass the CultureInfo param as new:

    public void SetCurrentLanguage()
    {
        var langService = DependencyService.Get<ILanguageService>();
        langService.SetApplicationLanguage(CultureInfo.CreateSpecificCulture("es-ES"));
    }
    
  • arturmullerarturmuller Member ✭✭✭

    Hi,

    Thanks for reply @FabricioBertani

    However, the suggested code does not work. It doesn't change the language.

    I implemented exactly your code like this:

    [assembly: Xamarin.Forms.Dependency(typeof(LanguageService))]
    namespace ProjectA.App.Droid.Locale
    {
        public class LanguageService : ILanguageService
        {
            public void SetApplicationLanguage(CultureInfo ci)
            {
                Thread.CurrentThread.CurrentCulture = ci;
                Thread.CurrentThread.CurrentUICulture = ci;
                CultureInfo.DefaultThreadCurrentCulture = ci;
                CultureInfo.DefaultThreadCurrentUICulture = ci;
            }
        }
    }
    

    Then I call it like this:

    var langService = DependencyService.Get<ILanguageService>();
    langService.SetApplicationLanguage(CultureInfo.CreateSpecificCulture("es-ES"));
    

    I have verified I have corresponding .resx file and it does changes the language correctly if I change the language in the device settings. I also had to implement this fix in order to get it to read correct .resx file https://github.com/xamarin/xamarin-android/issues/4664

    What am I missing?

    Best regards and thanks again

  • LandLuLandLu Member, Xamarin Team Xamurai

    resx file contains a culture property

    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Globalization.CultureInfo Culture {
        get {
            return resourceCulture;
        }
        set {
            resourceCulture = value;
        }
    }
    

    We could assign a culture to it to change the language:

    CultureInfo frenchUSCulture = new CultureInfo("fr-FR");
    AppResources.Culture = frenchUSCulture;
    


    Please note existed page won't be refreshed so I reenter the page to display the effect.

Sign In or Register to comment.