0

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?

Devanathan
  • 39
  • 5

1 Answers1

0

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

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • So what you want is to display the English style ? – ColeX Oct 13 '21 at 08:48
  • I asked Vice versa English to Vietnamese. But Thanks For IOS it works. I asked for both Android and IOS. For Android Please suggest me? – Devanathan Oct 13 '21 at 09:09
  • Is it possible to change at runtime if I switch language from English to Vietnamese and Vietnamese to English in app. Not the mobile language. – Devanathan Oct 18 '21 at 12:36
  • Yes , just use messaging center to call the code in specific platform project. – ColeX Oct 19 '21 at 05:31