Showing posts with label Cross Platform App Development. Show all posts

Multilingual Implementation - Xamarin.Forms



Introduction:

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

Coding Part:

Steps:

I have split the coding part into 4 steps as in the following.

  1. Creating new Xamarin.Forms Projects.
  2. Creating App Resource files for multi-language.
  3. Language Management
  4. Implementation of Multi-Lingual.

Step 1: Creating new Xamarin.Forms Projects

  • Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
  • Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

Step 2: Creating App Resource files for multi-language

  • Add one resource file as base file with the name 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

  • Then add resources with the same name with language specific values in each resource files as like in the below screenshot.

Step 3: Language Management

In this step, we will see how to handle the multilingual for Xamarin.Forms.

  • Get Device Language
    CultureInfo.InstalledUICulture
  • Get or Set the actual language
    CultureInfo language = new CultureInfo("ta");
    Thread.CurrentThread.CurrentUICulture = language;
    AppResources.Culture = language;

Step 4: Implementation of Multi-Lingual

  • Open your designer file and add the resources as shown in below code snippet.
    <?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:resource="clr-namespace:MultilingualXFSample.Resources"
                 x:Class="MultilingualXFSample.MainPage">
            <Label Text="{x:Static resource:AppResources.WelcomeText}"
                   FontSize="Large"/>
    </ContentPage>
  • By default, the app will take the base resource file. By passing the language code will takes the language specific resource file and we can set the language as shown in below code snippet in runtime
    CultureInfo language = new CultureInfo("ta");
    Thread.CurrentThread.CurrentUICulture = language;
    AppResources.Culture = language;
    Application.Current.MainPage = new NavigationPage(new MainPage());

Output

Default - English Tamil French

Download Code

You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.

Xamarin - Introduction


When considering how to build iOS and Android applications, many people think that the native languages, Objective-C, Swift, and Java, are the only choice. However, over the past few years, an entire new ecosystem of platforms for building mobile applications has emerged.

Xamarin is unique in this space by offering a single language – C#, class library, and runtime that works across all three mobile platforms of iOS, Android, and Windows Phone (Windows Phone’s native language is already C#), while still compiling native (non-interpreted) applications that are performant enough even for demanding games. 

Each of these platforms has a different feature set and each varies in its ability to write native applications – that is, applications that compile down to native code and that interop fluently with the underlying Java subsystem. For example, some platforms only allow apps to be built in HTML and JavaScript, whereas some are very low-level and only allow C/C++ code. Some platforms don’t even utilize the native control toolkit.  

In Androidmads, we will not cover the basics of Xamarin App development. But, we will cover the advanced concepts of Xamarin as well as we will assist you, if you find any help with Xamarin Basics. And also we will see about Flutter App development.