Overriding device locale for time and datepickers

ThomasNasbyThomasNasby USMember ✭✭

Our app is available in 4 languages. Our app will use the device's language, unless it's not one of the 4 options, where we resort to using English. Our users can also override the device setting by selecting a language inside the app. Everything works fine except for the DatePicker and TimePicker. We set the locale with the following code:

Resx.AppResources.Culture = ci; // set the RESX for resource localization
DependencyService.Get<ILocalize>().SetLocale(ci); // set the Thread for locale-aware methods

The app still uses the device's set language for only the pickers. EX: Device locale English, app locale Spanish, DatePicker shows months in English.

Is there any way to override this?

Thanks

Best Answer

  • ThomasNasbyThomasNasby US ✭✭
    Accepted Answer

    Ok, I've figured it out. It requires a custom renderer.

    iOS:

    var date = (UIDatePicker)Control.InputView;
    date.Locale = new Foundation.NSLocale(LocalizationService.GetCurrentThreadCultureInfo().TwoLetterISOLanguageName);
    

    Android:

    Locale locale = new Locale(LocalizationService.GetCurrentThreadCultureInfo().TwoLetterISOLanguageName);
    Control.TextLocale = locale;
    

    Both go inside a DatePickerRenderer.

Answers

  • HunumanHunuman GBMember ✭✭✭✭
    edited July 2017

    Hi @ThomasNasby

    Yes you can via setting the DatePicker.Format property to the ShortDateTimeFormat of the selected Locale.
    The same applies to the TimePicker.

    I hope this helps,

    Tim

  • ThomasNasbyThomasNasby USMember ✭✭

    I've already done that, and it does a good job formatting the text of the time/dates set (what you see when the widget isn't showing). I'm trying to change the language of the widget itself, as that is still in English.

  • ThomasNasbyThomasNasby USMember ✭✭
    Accepted Answer

    Ok, I've figured it out. It requires a custom renderer.

    iOS:

    var date = (UIDatePicker)Control.InputView;
    date.Locale = new Foundation.NSLocale(LocalizationService.GetCurrentThreadCultureInfo().TwoLetterISOLanguageName);
    

    Android:

    Locale locale = new Locale(LocalizationService.GetCurrentThreadCultureInfo().TwoLetterISOLanguageName);
    Control.TextLocale = locale;
    

    Both go inside a DatePickerRenderer.

  • HunumanHunuman GBMember ✭✭✭✭

    Hi @ThomasNasby

    Glad you got it sorted.
    It raises the question is that a bug?

    XF supports localisation out of the box, so I would have expected it to do that automagically.

    Tim

  • ThomasNasbyThomasNasby USMember ✭✭
    edited July 2017

    I'd say it is. When I set the locale of the app (which I used this guide) you explicitly set the UICulture.

    public void SetLocale(CultureInfo ci)
            {
                Thread.CurrentThread.CurrentCulture = ci;
                Thread.CurrentThread.CurrentUICulture = ci;
    
                Console.WriteLine("CurrentCulture set: " + ci.Name);
            }
    

    I don't know why a DatePicker and TimePicker (and who knows what else) would be exempt or use the device settings as opposed to the app's settings. The ResX files use the language I set, as expected.

    Do you know how to elevate this to a possible bug?

  • OmarHassan.2150OmarHassan.2150 USMember ✭✭
    edited January 2018

    @Hunuman said:
    Hi @ThomasNasby

    Yes you can via setting the DatePicker.Format property to the ShortDateTimeFormat of the selected Locale.
    The same applies to the TimePicker.

    I hope this helps,

    Tim

    @Hunuman said:
    Hi @ThomasNasby

    Glad you got it sorted.
    It raises the question is that a bug?

    XF supports localisation out of the box, so I would have expected it to do that automagically.

    Tim

    I followed your solution but the picker language still English, Could you please help?

  • IsmaelMendoncaIsmaelMendonca USMember ✭✭

    I followed your solution but the picker language still English, Could you please help?

    Hello guys!

    Actually this is quite simple, the iOS app gets the localization and region setted into the device system preferences to render the texts and whatever else, but the hidden trick is that you need to configure that into the info.plist.

    Before the </dict> tag put these tags:

    <key>CFBundleLocalizations</key>
    <array>
    <string>pt-BR</string>
    <string>en</string>
    </array>

    If you don't do this configuration the app will not be allowed to access the system preferences info.

    Ps.: Into the array put the languages key that you want.

    Credits:
    "blog.verslu.is/xamarin/xamarin-forms-xamarin/localization-default-ios-controls/"

Sign In or Register to comment.