Multilingual Implementation Using Xamarin.Forms

Multilingual Implementation Using Xamarin.Forms 
 

Introduction

 
In this tutorial, we will learn how to implement Multilingual in Xamarin.Forms without using external plugins or platform wise implementation with dependency service. The latest Xamarin forms offers .net standard to implement multilingual. So without further delay, we will skip into the coding part of the article.
 

Steps

 
I have split the coding part into 4 steps as in the following.
 
Step 1 - Creating new Xamarin.Forms Projects.
 
Step 2 - Creating App Resource files for multi-language.
 
Step 3 - Language Management
 
Step 4 - Implementation of Multilingual.
 
Step 1 - Creating new Xamarin.Forms Projects
  • Create a new project by selecting New Project. Select Xamarin Cross Platform App and click OK.

    Multilingual Implementation Using Xamarin.Forms

  • Then Select Android, iOS and UWP Platforms as shown below with Project template as "Blank" and Click OK.

    Multilingual Implementation Using Xamarin.Forms
Step 2 - Creating App Resource files for multiple languages
  • Add one resource file as base file with the name as your preferred name. In my case, I kept the name of the file as “AppResources.resx” and it will be used as default language file.
  • Then add one resource file for each language you want to support. We must follow a specific naming convention for each file as the base resources file followed by a period (.) and then the language code.
  • For Example
Language
Language Code
File Name
Tamil
ta
AppResources.ta.resx
French
fr
AppResources.fr.resx

Multilingual Implementation Using Xamarin.Forms
  • Then add resources with the same name with language specific values in each resource files as in the below screenshot.

    Multilingual Implementation Using Xamarin.Forms
Step 3 - Language Management
 
In this step, we will see how to handle  multilingual for Xamarin.Forms.
 
Get Device Language
  1. CultureInfo.InstalledUICulture  
Get or Set the actual language
  1. CultureInfo language = new CultureInfo("ta");  
  2. Thread.CurrentThread.CurrentUICulture = language;  
  3. AppResources.Culture = language;  
Step 4 - Implementation of Multi-Lingual
  • Open your designer file and add the resources as shown in the below code snippet.
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
    3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
    4.              xmlns:resource="clr-namespace:MultilingualXFSample.Resources"  
    5.              x:Class="MultilingualXFSample.MainPage">  
    6.         <Label Text="{x:Static resource:AppResources.WelcomeText}"  
    7.                FontSize="Large"/>  
    8. </ContentPage>  
  • By default, the app will take the base resource file. By passing the language code it will take the language specific resource file and we can set the language as shown in the below code snippet in runtime.
    1. CultureInfo language = new CultureInfo("ta");  
    2. Thread.CurrentThread.CurrentUICulture = language;  
    3. AppResources.Culture = language;  
    4. Application.Current.MainPage = new NavigationPage(new MainPage());   
Output
 
Default - English
Tamil
French
Multilingual Implementation Using Xamarin.Forms
Multilingual Implementation Using Xamarin.Forms
Multilingual Implementation Using Xamarin.Forms
 
Download Code
 
You can download the code from GitHub. If you have doubts, feel free to post a comment. If you like this article and it  is useful to you, do like, share the article & star the repository on GitHub.


Similar Articles