Localize a DatePicker on Android using .Net Maui

Kaouthar GR AOUICHAOUI 80 Reputation points
2024-03-18T12:19:47.7466667+00:00

Hi , i'm working with .net Maui 8 , and i want to use datepicker. it display name of days, months in English or i want it in french.

i refer to learn page

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/datepicker?view=net-maui-8.0

but i found only localize a DatePicker on windows. but not in Android platform.

i add this code to App.xaml.cs but nothing change.

var culture = new CultureInfo("fr-FR"); // Replace with your desired culture or language code

CultureInfo.DefaultThreadCurrentCulture = culture;

CultureInfo.DefaultThreadCurrentUICulture = culture;

please can any help me to change language from english to Frensh in datepicker on Android platform.

thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,911 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,586 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,005 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,471 Reputation points Microsoft Vendor
    2024-03-19T07:24:34.49+00:00

    Hello,

    This issue is reported in the MAUI github:DatePicker is not localization in the calendar view. #15744 Please track on this issue in the above link.

    You can use following DatePickerHandler in the app.xaml.cs, except the cancel and Ok button's text will not be changed, other content will be changed as normal.

       Microsoft.Maui.Handlers.DatePickerHandler.Mapper.PrependToMapping("MyCustomization", (handler, view) =>
                {
    #if ANDROID
                    Java.Util.Locale locale = new Java.Util.Locale("fr-FR");
                    handler.PlatformView.TextLocale = locale;
                    Android.Content.Res.Configuration config = new Android.Content.Res.Configuration();
                    config.Locale = locale;
                    Java.Util.Locale.SetDefault(Java.Util.Locale.Category.Format, locale);
                    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.Configuration.SetLocale(locale);
                    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.Configuration.Locale = locale;
                    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.UpdateConfiguration(config, Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.DisplayMetrics);
                    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.UpdateConfiguration(config, Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Resources.DisplayMetrics);
    #endif
                });
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.