How to perfectly localize DatePicker, TimePicker in Xamarin

39 Views Asked by At

I want that when i change language in my app, the picker default text will change to localized text

enter image description here

The above picture is the default DatePicker (i changed only Cancel, Done Button Text)

if our user want to use Korean, the user should change language in our application settings

enter image description here

but it couldn' replace window's action bar part

enter image description here

in system settings, when i changed language

enter image description here

i can get a perfectly localized datepicker

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App2.Resources;
using Java.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.CommunityToolkit.Helpers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using DatePicker = Xamarin.Forms.DatePicker;

[assembly: ExportRenderer(typeof(Xamarin.Forms.DatePicker), typeof(App2.Droid.CustomRenderer.LocalizationDatePicker))]
namespace App2.Droid.CustomRenderer
{
    public class LocalizationDatePicker : DatePickerRenderer
    {
        DatePickerDialog pickerDialog;
        private Xamarin.Forms.DatePicker datePicker;
        public LocalizationDatePicker(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);
            //Disposing
            if (e.OldElement != null)
            {
                datePicker = null;
            }
            //Creating
            if (e.NewElement != null)
            {
                datePicker = e.NewElement;
            }

            Locale locale = new Java.Util.Locale(LocalizationResourceManager.Current.CurrentCulture.Name.Split('-')[0]);
            Control.TextLocale = locale;
            Android.Content.Res.Configuration config = new Android.Content.Res.Configuration();
            config.SetLocale(locale);
            Locale.SetDefault(Locale.Category.Display, locale);
            Resources.Configuration.SetLocale(locale);
        }

        protected override DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
        {
            pickerDialog = new DatePickerDialog(Context, (o, e) =>
            {
                datePicker.Date = new DateTime(e.Year, e.Month, e.DayOfMonth, 0, 0, 0, LocalizationResourceManager.Current.CurrentCulture.Calendar)  ;
                ((IElementController)datePicker).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
            }, year, month, day);
       
            //ok
            pickerDialog.SetButton((int)DialogButtonType.Positive, AppResources.Accept, OnDone);

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

            return pickerDialog;
        }

        private void OnCancel(object sender, DialogClickEventArgs e)
        {
            datePicker.Unfocus();
        }

        private void OnDone(object sender, DialogClickEventArgs e)
        {
            datePicker.Date = ((DatePickerDialog)sender).DatePicker.DateTime;
            datePicker.Unfocus();
        }
    }
}

this is LocalizedDatePicker CustomRenderer Code

Please inform how to customize Datepicker's Dialog ActionBar? header? or how to perfectly localize DatePicker

0

There are 0 best solutions below