How to change xamarin application language

Anas Guibene 491 Reputation points
2021-08-05T12:20:57.893+00:00

I have a multilanguage application (English, french, dutch) that works with the default system language.
How can i change the language of the application after selecting the language from a Combobox?

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-08-06T05:25:20.183+00:00

    Hi AnasGuibene,

    Welcome to our Microsoft Q&A platform!

    To achieve multilanguage in Xamarin.Forms, you can use Xamarin Community Toolkit TranslateExtension.

    Here are the steps you can refer to.

    First, create the resource file for each language and add "Name-Value" pair to it.
    121038-image.png

    AppResource.es.resx
    121026-image.png

    AppResource.es.resx
    121039-image.png

    Second, modify the App.xaml.cs as follows.

    public App()  
    {  
        InitializeComponent();  
      
        LocalizationResourceManager.Current.PropertyChanged += Current_PropertyChanged;  
        LocalizationResourceManager.Current.Init(AppResource.ResourceManager);  
      
        MainPage = new MainPage();  
    }  
      
    private void Current_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)  
    {  
        AppResource.Culture = LocalizationResourceManager.Current.CurrentCulture;  
    }  
    

    And here is the view "MainPage.xaml"

    <ContentPage.BindingContext>  
        <local:MainPageViewModel/>  
    </ContentPage.BindingContext>  
    
    <StackLayout>  
        <Label Text="{xct:Translate Content}"/>  
        <Button Text="{xct:Translate ChangeLanguage}" Command="{Binding ChangeLanguageCommand}" />  
    </StackLayout>  
    

    Last, create the viewmodel for MainPage.

    class MainPageViewModel  
    {  
        public class LanguagePair  
        {  
            public Func<string> name;  
            public string value;  
        }  
      
        public MainPageViewModel()  
        {  
            CurrentLanguage = new LocalizedString(() => GetCurrentLanguageName());  
      
            ChangeLanguageCommand = new AsyncCommand(ChangeLanguage);  
            languageMapping = new List<LanguagePair>();  
            languageMapping.Add(new LanguagePair { name = () => AppResource.English, value = "en" });  
            languageMapping.Add(new LanguagePair { name = () => AppResource.Spanish, value = "es" });  
        }  
      
        List<LanguagePair> languageMapping { get; }  
        public LocalizedString CurrentLanguage { get; }  
        public ICommand ChangeLanguageCommand { get; }  
        private string GetCurrentLanguageName()  
        {  
            string name = languageMapping.SingleOrDefault(m => m.value == LocalizationResourceManager.Current.CurrentCulture.TwoLetterISOLanguageName).name.ToString();  
      
            return name != null ? name : LocalizationResourceManager.Current.CurrentCulture.DisplayName;  
        }  
      
        async Task ChangeLanguage()  
        {  
            string selectedName = await Application.Current.MainPage.DisplayActionSheet(  
                AppResource.ChangeLanguage,  
                null, null,  
                languageMapping.Select(m => m.name()).ToArray());  
            if (selectedName == null)  
            {  
                return;  
            }  
      
            string selectedValue = languageMapping.Single(m => m.name() == selectedName).value;  
            LocalizationResourceManager.Current.CurrentCulture = selectedValue == null ? CultureInfo.CurrentCulture : new CultureInfo(selectedValue);  
        }  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,206 Reputation points
    2021-08-05T13:04:19.037+00:00

    I think you can take a look to this video: watch

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.