xamarinxamarin.formsxamarin.androidxamarin.ioscustom-renderer

How to display months and days in Vietnamese in DatePickerDialog?


How to display months and days in Vietnamese in DatePickerDialog for Android and DatePicker in IOS when the app language to Vietnamese in Xamarin forms

Android:

protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
        {
            pickerDialog = new DatePickerDialog(Context, (o, e) =>
            {
                datePicker.Date = e.Date;
                ((IElementController)datePicker).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
            }, year, month, day);

            //ok
            pickerDialog.SetButton((int)DialogButtonType.Positive, AppResources.AlertDismiss, OnDone);

            //cancel
            pickerDialog.SetButton((int)DialogButtonType.Negative, AppResources.PickerCancelText, OnCancel);

            return pickerDialog;
        }

IOS: suggest me how to do in IOS?


Solution

  • If I understand correctly , do you want to show English formatting for the DatePicker?

    If so you can modify the Locale for the datepicker in custom renderer .

    iOS Solution

    
    [assembly: ExportRenderer(typeof(DatePicker), typeof(MyRenderer))]
    namespace FormsApp.iOS
    {
        class MyRenderer : DatePickerRenderer
        {
    
            protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
            {
                base.OnElementChanged(e);
              
                if(Control != null)
                {
                    UIDatePicker picker = Control.InputView as UIDatePicker;
                    picker.Locale = new NSLocale("us");
                }
            }
        }
    }
    

    Before

    enter image description here

    After

    enter image description here


    If you want the Datepicker only display month and day for selecting item , it could be a little complex , because iOS does not provide the option/mode to achieve that , you need to create a view and controller that implement the UIPickerViewDelegate and create your own .

    Refer to

    https://stackoverflow.com/a/412754/8187800.

    https://stackoverflow.com/a/24971192/8187800.


    Android Solution

    Add the following code into OnCreate in MainActivity class .

     protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
                //this code 
                Locale locale = Locale.Us;
                Locale.SetDefault(Locale.Category.Format, locale);
                Configuration config = this.Resources.Configuration;
                config.SetLocale(locale);
                CreateConfigurationContext(config);
                    
                LoadApplication(new App());
            }
    

    Before

    enter image description here

    ###After

    enter image description here

    Refer to

    DatePickerDialog in arabic language

    Set Android DatePicker title language