New to Telerik UI for Xamarin? Download free 30-day trial

Culture info for the app

Environment

Product Version 2021.2.728.1
Product Progress® Telerik® UI for Xamarin

Description

This article will show you how to apply current culture (localization) on application level (app localization). For example if the device culture is set to Spanish or German or French, and you want to have different app culture (localization) than the device culture, you can explicitly set culture info on app level.

Solution

Android

On Android: inside the MainActivity class, implement SetLocale() method to change the culture info.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        this.SetLocale();
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);


        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    void SetLocale()
    {

        CultureInfo ci = new CultureInfo("en-US");

        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;

        Console.WriteLine("CurrentCulture set: " + ci.Name);
    }
}

iOS

on iOS inside the AppDelegate.cs file override the FinishedLaunching method

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    this.SetLocale();
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

private void SetLocale()
{
    CultureInfo ci = new CultureInfo("en-US");

    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
}
In this article
Not finding the help you need? Improve this article