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".
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.
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”.
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.
So our Spanish file contains Spanish translation. Now, use this resource in XAML page.
XAML
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:CSharpCorner"
- x:Class="CSharpCorner.MainPage"
- xmlns:Resource="clr-namespace:CSharpCorner"
- >
-
- <Label Text="{x:Static Resource:ApplicationResource.BodyText}"
- x:Name="label"
- VerticalOptions="Center"
- HorizontalOptions="Center"
- FontSize="Large"/>
-
- </ContentPage>
You can also set the resource from C#.
C#
- using Xamarin.Forms;
-
- namespace CSharpCorner
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- label.Text = ApplicationResource.BodyText;
- }
- }
- }
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
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.