Support Multiple Languages In Xamarin.Forms Application

Introduction

In this article, we are going to make an application through which we can support multiple languages in our Xamarin.Forms application. We are going to achieve our goal through globalizing the Xamarin.Forms code by adding and using string resources in PCL project.

Implementation

Firstly, create a Xamarin.Forms PCL project. If you are new to Xamarin then go to this link to learn to create a Xamarin.Forms application. Here I assumed that your project is created and ready to develop this sample project.

After creating PCL project, now we are going to add a resource file which contains string resources to be used in our application.

Click on Add New Item >> Resource File. Rename it and click "Add".

Xamarin

Now, add some resource in your resource file which we are going to use in our application. Here, you are going to add a resource name, its values, and comments. After this, change the access modifier of the resource as public, as shown in the picture.

Xamarin

So, here our default resource file is created. Now, make another resource file in the language you want to support in your application. The naming convention of the other supportive language must be started from the default resource file name followed by “.” And then use language specific code, just like this

  • AppResources.fr.resx - French language translations.
  • AppResources.es.resx - Spanish language translations.
  • AppResources.de.resx - German language translations.
  • AppResources.ja.resx - Japanese language translations.
  • AppResources.zh-Hans.resx - Chinese (Simplified) language translations.
  • AppResources.zh-Hant.resx - Chinese (Traditional) language translations.
  • AppResources.pt.resx - Portuguese language translations.
  • AppResources.pt-BR.resx - Brazilian Portuguese language translations.

Now here, we are going to support Spanish in our application. So, make the other resource file with a name as “ApplicationResource.es.resx”.

Xamarin

Now, with the same name, write these values in Spanish. So, our ApplicationResource.es.resx file will look like this. And don’t forget to make it public.

Xamarin

So our Spanish file contains Spanish translation. Now, use this resource in XAML page.

XAML

  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:local="clr-namespace:CSharpCorner"  
  5.              x:Class="CSharpCorner.MainPage"  
  6.              xmlns:Resource="clr-namespace:CSharpCorner"    
  7.              >  
  8.   
  9.     <Label Text="{x:Static Resource:ApplicationResource.BodyText}"   
  10.            x:Name="label"  
  11.            VerticalOptions="Center"   
  12.            HorizontalOptions="Center"  
  13.            FontSize="Large"/>  
  14.   
  15. </ContentPage>  

You can also set the resource from C#.

C#

  1. using Xamarin.Forms;  
  2.   
  3. namespace CSharpCorner  
  4. {  
  5.     public partial class MainPage : ContentPage  
  6.     {  
  7.         public MainPage()  
  8.         {  
  9.             InitializeComponent();  
  10.             label.Text = ApplicationResource.BodyText;  
  11.         }  
  12.     }  
  13. }  

Now, to test our application, change the mobile language to Spanish and you will see some Spanish text; otherwise, the default English text is shown.

Output

Xamarin Xamarin

In the first emulator, the English language is set as default, whereas, in the second emulator, the default language is set Spanish.  This means that the application is using our Spanish resource file to show the text.

Up Next
    Ebook Download
    View all
    Learn
    View all