Xamarin Unable to change language during run time(multi language app)

Srinath Mannam 291 Reputation points
2022-03-09T23:28:13.36+00:00

Language localization isn't working for me. The app has been implemented with the
AppShell
concept.

  • The user logs in and goes to the main menu. By default, the language will be selected as per the system (Let's assume it is English).
    181661-image.png
  • After logging in the user has a flyout menu option called language settings, where one can change the language. Let's assume he selected German over here.
    181652-image.png
  • Since the main menu is already rendered in English at the begining. Even if the user changes language in settings. The main menu doesn't update to the new language. It shows the English language only.
    181671-image.png
  • If the user navigates to any other screen which the person hasn't opened yet then that specific screen will have the newly selected language.
    181599-image.png

I am not sure why this is happening. Can someone help me with the same?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2022-03-10T07:42:42.397+00:00

    Hello,

    I have a viable solution that has been tested in Android 10 and later.

    Here is the solution you can try it.

    Step1. After creating resource files, you need to create TranslateExtension.cs and Translator.cs in your project. The following is their code:

    TranslateExtension.cs:

       public class TranslateExtension : IMarkupExtension<BindingBase>  
           {  
               public TranslateExtension(string text)  
               {  
                   Text = text;  
               }  
         
               public string Text { get; set; }  
         
               object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)  
               {  
                   return ProvideValue(serviceProvider);  
               }  
         
               public BindingBase ProvideValue(IServiceProvider serviceProvider)  
               {  
                   var binding = new Binding  
                   {  
                       Mode = BindingMode.OneWay,  
                       Path = $"[{Text}]",  
                       Source = Translator.Instance,  
                   };  
                   return binding;  
               }  
           }  
    

    Translator.cs:

       public class Translator :INotifyPropertyChanged  
           {  
               public string this[string text]  
               {  
                   get  
                   {  
           // Here is the name of your Resource file.  
                       return AppResources.ResourceManager.GetString(text, CultureInfo);  
                   }  
               }  
         
               public CultureInfo CultureInfo { get; set; }  
         
               public static Translator Instance { get; } = new Translator();  
         
               public event PropertyChangedEventHandler PropertyChanged;  
         
               public void Invalidate()  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));  
               }  
           }  
    

    Step2. You need to use this style Binding [Task], Source={x:Static vm:Translator.Instance}(the Task is a Name in your resource file) in your XAML file to binding.

    Step3. In your PickerEvent, for example, if you want to convert language to German, you can add the following code to the event:

       Translator.Instance.CultureInfo = new CultureInfo("de");  
       Translator.Instance.Invalidate();  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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 person found this answer helpful.

  2. fabs cleanz 1 Reputation point
    2022-11-10T13:19:25.983+00:00

    Nice!
    But how to change the Flyout menu item texts? They remain the same.