Tuesday 6 June 2023

How to handle multilingual in Xamarin forms?

 

How to handle multilingual in Xamarin forms?

In this blog, we will be going to talk about how to implement multilingual in xamarin forms. Multilingual means user requires the multiple languages in their application because there are many people from everywhere to utilizethe application, at that time multilingual feature is the most useful one.

Localization is the process of adapting an application to satisfy the precise language or cultural requirements of a target market. To accomplish localization, text and pictures in an application may need to be translated into multiple languages.

How to implement multilingual using xamarin forms?


You can implement multilingual by creating resources for each language and bundling them with the internationalize app. All you've got to try to do is to make a resource file for every language. These resources have an inventory of key-value pairs of words, phrases, or sentences.

For example, you've got a resource file for English language and you've got the key of SubmitText with a worth of “Welcome”. And furthermore,you may have a resource file for Filipino which also features a SubmitText key but with a worth of “Maligayangpagdating”. After creating these resources, you’ll now use it on your app. The app will select its corresponding resource counting on the phone’s language. If there's no corresponding resource, it'll use by default language, which will be English.

 

Step 1: First Create the xamarin project

Step1: Create a new project by selecting the xamarin forms project.

xamarin_first
Fig 1: Create a xamarin project.

Step 2: Next, select the blank template and platform depending on your requirements.

template
Fig 2: Select the template and platform

Step 2: After creating the project, create a new folder and assign the name as Resource.Then, create a resource file.

Step 1: Right-click on the solution explorer then select

Add ->New Item-> select General -> select the resource dictionary file -> assign the suitable name (Example:AppResources.resx) ->Then click Ohk.
xamarinResource
Fig 3: Resource file.

Example of Assign the name:
LanguageCode of LanguageFile Name
FranceFrAppResources.fr.resx
SpanishSpAppResourcec.sp.resx
Table 1: Resource file name
 

Step 3:Create the resource file if you need to translate the content of your required language.

The Access Modifier sink setting determines how Visual Studio generates the category want to access resources. Setting the Access Modifier to Public or Internal leads to a generated class with the required accessibility level. Setting the Access Modifier to No code generation doesn't generate a category file. The default resource file should be configured to get a category file, which ends up during a file with the designer.cs extension being added to the project.

Once through with our default resource file, we will now create resource files for other languages that we would like to support. For each language you’re supporting, you'll get to add a resource file with the name: “Resource. {culture name}.resx”. You can find an inventory of culture names here. You will able to add equivalent keys that will be added in our default resource to give surety for app that will have translations for every text.

The translation file uses equivalent Name values laid out in the default file but contains Spanish language strings within the Value column. Additionally, the Access Modifier is about No code generation.

 

Example

1) File Name: AppResources.resx

 
NameValue
WelcomeToXamarinFormsWelcome to xamarin forms
Table 2: AppResources.resx

2) File Name: AppResource.fr.resx(For the French Language)

 
NameValue
WelcomeToXamarinFormsBienvenue dans les formulaires Xamarin
Table 3: AppResources.fr.resx
 

Step 4: Handel the Multilingual Implementation.

First, you have to set the actual language.

Code:

CultureInfo info = new CultureInfo("fr");
Thread.CurrentThread.CurrentUICulture = info;
AppResource.Culture = info;
Application.Current.MainPage = new NavigationPage(new MainPage());

Step 5: Implement the Multilingual in the XAML file.

If you have used this file before, you need to add the namespace.

xmlns:resource=” clr-namespace:DemoOfMultilingual”;

Then use the file to be shown below:

Code:

roadmap-scrum

Step 6: Run the project multilingual.

Example of the Multilingual Implementation.

using DemoOfMultilingual.Resx;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace DemoOfMultilingual
{
public partial class MainPage :ContentPage
{
public MainPage()
{
InitializeComponent();
CultureInfo.InstalledUICulture;
CultureInfo info = new CultureInfo("fr");
Thread.CurrentThread.CurrentUICulture = info;
AppResource.Culture = info;
Application.Current.MainPage = new NavigationPage(new MainPage());
}

Step 1:Create the xamarin project.

Step 2:After creating the project to create the resource file and assign them a suitable name.


Example: AppResources.resx
NameValueComment
ClickMeClick Me 
Welcome_to_xamarin_formsWelcome to xamarin forms 
Table 4: AppResources.resx

Second file: AppResources.fr.resx. (French)
NameValueComment
ClickMeCliquez sur moi 
Welcome_to_xamarin_formsBienvenue dans les formulaires Xamarin 
Table 5: AppResources.fr.resx

Third File: AppResources.gr.resx(German)
NameValueComment
ClickMeKlick mich 
Welcome_to_xamarin_formsWillkommen bei Xamarin-Formularen 
Table 6: AppResources.gr.resx

Step 3: You can assign the name in the XAML page as shown below File name:Mainpage.xaml
mainpage

Class file:Mainpage.cs Another way to perform the control using the file below:

Searching for Reliable Xamarin Mobile App Development Company? Contact Now.


File Name: App.xaml.cs
using DemoOfMultilingual.Resx;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace DemoOfMultilingual
{
public partial class App: Application
{
public App ()
{
InitializeComponent();
MainPage = new ContentPage()
{
Content = new Label()
{
Text = AppResources.Welcome_to_xamarin_forms,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Color.Red,
FontSize = 22
}
};
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}

Output of the project


English Language

EnglishLangauge

French Language

FrenchLanguager

Conclusion


As you can see, handling Multilingual in Xamarin.Forms are sort of easy even within the most complex scenarios, there are easy ways to urge it to work for your needs. As a further recommendation, we encourage you to ascertain these great tips within the Xamarin documentation about Localization best practices which can assist you to possess more consistent code. Both are really useful tools when it involves localization-related tasks.

Content source: https://www.ifourtechnolab.com/blog/how-to-handle-multilingual-in-xamarin-forms



No comments:

Post a Comment

Unleash the Power of Explainable AI in Banking Software Development

  What exactly is Explainable AI? Explainable AI (XAI) is a subfield of Artificial intelligence (AI) that focuses on developing algorithms a...