Implemented Localization In A Xamarin.Forms

I am going to create localization in a Xamarin.Forms app. Whenever we are creating a Xamarin forms app, I want to need to change the Current Language and that time was stuck in the app, after some search I got the solution to this problem. So in this blog, I am going to implement the localization in Xamarin forms app.

Today, I am going to create localization in a Xamarin.Forms app. Whenever I create a Xamarin.Forms app, I want to change the current language and that time, I get stuck in the app. After doing some research I got the solution to the problem. So in this blog, I am going to implement the localization in the Xamarin.forms app.

 

Implementation

First, we have to create a project with a name LocalizationDemo.

Open VS2017 >> select file >> Create Project >> give the project name and then set the path and hit enter.
 
Implemented Localization In A Xamarin.Forms App

Now, a window appears, then you have to select a blank template. Then choose a platform like Android or iOS and a code-sharing strategy like .NET Standard then hit enter.

Implemented Localization In A Xamarin.Forms App 

In PCL Project, we can create a folder with the name Localization,

Implemented Localization In A Xamarin.Forms App 

Now, in the Localization folder, we can create a Class with a name CultureChangedMessage and LocalizedResources.

CultureChangedMessage.cs

  1. public class CultureChangedMessage  
  2. {  
  3. public CultureInfo NewCultureInfo { get; private set; }  
  4. public CultureChangedMessage(string lngName)  
  5. this(new CultureInfo(lngName))  
  6. { }  
  7.     public CultureChangedMessage(CultureInfo newCultureInfo)  
  8.     {  
  9.         NewCultureInfo = newCultureInfo;  
  10.     }  
  11. }  

CultureInfo

The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions.

LocalizedResources.cs
  1. public class LocalizedResources : INotifyPropertyChanged  
  2. {  
  3. const string DEFAULT_LANGUAGE = "en";  
  4.     readonly ResourceManager ResourceManager;  
  5.     CultureInfo CurrentCultureInfo;  
  6.   
  7.     public string this[string key]  
  8.     {  
  9.         get  
  10.         {  
  11.             return ResourceManager.GetString(key, CurrentCultureInfo);  
  12.         }  
  13.     }  
  14.   
  15.     public LocalizedResources(Type resource, string language = null)  
  16.         : this(resource, new CultureInfo(language ?? DEFAULT_LANGUAGE))  
  17.     { }  
  18.   
  19.     public LocalizedResources(Type resource, CultureInfo cultureInfo)  
  20.     {  
  21.         CurrentCultureInfo = cultureInfo;  
  22.         ResourceManager = new ResourceManager(resource);  
  23.   
  24.         MessagingCenter.Subscribe<object, CultureChangedMessage>(this,  
  25.             string.Empty, OnCultureChanged);  
  26.     }  
  27.   
  28.     private void OnCultureChanged(object s, CultureChangedMessage ccm)  
  29.     {  
  30.         CurrentCultureInfo = ccm.NewCultureInfo;  
  31.         PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs("Item"));  
  32.     }  
  33.   
  34.     public event PropertyChangedEventHandler PropertyChanged;  
  35. }  

ResourceManager:provides convenient access to culture-specific resources at run time.

Now, we can create a Resources folder and in this folder, we can add .resx

  • LocalizationDemoResources.fr.resx for (fr = French)

    • Add String Name and Value
      PickLng = Choisir votre langue
      Settings = Paramètres
      Welcome = Bienvenue! Bonjour!
  • LocalizationDemoResources.nl.resx for (nl = Dutch)

    • Add String Name and Value
      PickLng = Selecteer uw taal:
      Settings = Instellingen
      Welcome = Welkom! Hallo!


  • LocalizationDemoResources.resx for (English)

    • Add String Name and Value
      PickLng = Choose your language:
      Settings = Settings
      Welcome = Welcome! Hello!

Now, we can set the default Language in App.cs

  1. public static string CurrentLanguage = "EN";  

Now, Create ViewModels Folder and add ViewModelClassess.

MainPageViewModel, SettingsViewModel, ViewModelBase.

ViewModelBase.cs

  1. public class ViewModelBase : INotifyPropertyChanged  
  2. {  
  3. public LocalizedResources Resources  
  4. {  
  5. get;  
  6. private set;  
  7. }  
  8.     public ViewModelBase()  
  9.     {  
  10.         Resources = new LocalizedResources(typeof(LocalizationDemoResources), App.CurrentLanguage);  
  11.     }  
  12.   
  13.     public void OnPropertyChanged([CallerMemberName]string property = null)  
  14.     {  
  15.         PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(property));  
  16.     }  
  17.   
  18.     public event PropertyChangedEventHandler PropertyChanged;  
  19. }  

MainPageViewModel.cs

  1. public class MainPageViewModel : ViewModelBase  
  2. { }  
  3. SettingsViewModel.cs  
  4. public List Languages { get; set; } = new List()  
  5. {  
  6. "EN",  
  7. "NL",  
  8. "FR"  
  9. };  
  10.     private string _SelectedLanguage;  
  11.   
  12.     public string SelectedLanguage  
  13.     {  
  14.         get { return _SelectedLanguage; }  
  15.         set  
  16.         {  
  17.             _SelectedLanguage = value;  
  18.             SetLanguage();  
  19.         }  
  20.     }  
  21.   
  22.     public SettingsViewModel()  
  23.     {  
  24.         _SelectedLanguage = App.CurrentLanguage;  
  25.     }  
  26.   
  27.     private void SetLanguage()  
  28.     {  
  29.         App.CurrentLanguage = SelectedLanguage;  
  30.         MessagingCenter.Send<object, CultureChangedMessage>(this,  
  31.                 string.Empty, new CultureChangedMessage(SelectedLanguage));  
  32.    }  

Now, change the MainPage design and binding with MainPageViewModel.cs

The last step is adding SettingsPage and setting a design than binds with SettingsViewModel.cs

 

Hopefully, this article was useful.