question

AnasGuibene avatar image
0 Votes"
AnasGuibene asked LeonLu-MSFT commented

How to change xamarin application language

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?

dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered LeonLu-MSFT commented

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.



image.png (2.4 KiB)
image.png (7.0 KiB)
image.png (7.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AnasGuibene May I know if you have got any chance to check Kyle's answer? I am glad to help if you have any other questions.

1 Vote 1 ·
alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.