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 have got a 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. Here, 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. public string this[string key]
  7. {
  8. get
  9. {
  10. return ResourceManager.GetString(key, CurrentCultureInfo);
  11. }
  12. }
  13. public LocalizedResources(Type resource, string language = null)
  14. : this(resource, new CultureInfo(language ?? DEFAULT_LANGUAGE))
  15. { }
  16. public LocalizedResources(Type resource, CultureInfo cultureInfo)
  17. {
  18. CurrentCultureInfo = cultureInfo;
  19. ResourceManager = new ResourceManager(resource);
  20. MessagingCenter.Subscribe<object, CultureChangedMessage>(this,
  21. string.Empty, OnCultureChanged);
  22. }
  23. private void OnCultureChanged(object s, CultureChangedMessage ccm)
  24. {
  25. CurrentCultureInfo = ccm.NewCultureInfo;
  26. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item"));
  27. }
  28. public event PropertyChangedEventHandler PropertyChanged;
  29. }

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!

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

  1. public static string CurrentLanguage = "EN";

Now, create a ViewModels folder and add the ViewModel Classes - 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. public void OnPropertyChanged([CallerMemberName]string property = null)
  13. {
  14. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
  15. }
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. }

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. public string SelectedLanguage
  12. {
  13. get { return _SelectedLanguage; }
  14. set
  15. {
  16. _SelectedLanguage = value;
  17. SetLanguage();
  18. }
  19. }
  20. public SettingsViewModel()
  21. {
  22. _SelectedLanguage = App.CurrentLanguage;
  23. }
  24. private void SetLanguage()
  25. {
  26. App.CurrentLanguage = SelectedLanguage;
  27. MessagingCenter.Send<object, CultureChangedMessage>(this,
  28. string.Empty, new CultureChangedMessage(SelectedLanguage));
  29. }

Now, change the MainPage design and bind it with MainPageViewModel.cs.

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

Hopefully, this article was useful.