xamarin-forms-docs/knowledge-base/culture-info-app-level.md

2.4 KiB

title description type page_title slug position tags ticketid res_type
Culture info for the app apply culture info on app level how-to Change culture info on app level culture-info-app-level culture info, current culture, localization, app 1531416 kb

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;
}