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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

So what you want is to display the English style ?
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?
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.
Yes , just use messaging center to call the code in specific platform project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.