dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.89k stars 1.69k forks source link

DatePicker is not localization in the calendar view. #15744

Open saiganesh-sakthivel opened 1 year ago

saiganesh-sakthivel commented 1 year ago

Description

We want to localize the calendar in the datepicker. We have set the cultureInfo as fr (French) for the application. but it does not change the language as french locale the view.

Steps to Reproduce

  1. Run the sample.
  2. Click the datepicker.
  3. calendar in the view is not localized.

Link to public reproduction project repository

https://github.com/dotnet/maui/files/11798627/MauiApp1.zip

Version with bug

7.0.49

Last version that worked well

6.0

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

iOS 16, android 11, windows

Did you find any workaround?

No response

Relevant log output

No response

drasticactions commented 1 year ago

@saiganesh-sakthivel This is not specific to the DatePicker control, it's how you're setting the language in code.

    public App()
    {
        InitializeComponent();
        var culture = new CultureInfo("fr-FR"); // Replace with your desired culture or language code
        CultureInfo.DefaultThreadCurrentCulture = culture;
        CultureInfo.DefaultThreadCurrentUICulture = culture;
        MainPage = new AppShell();
    }

Setting the CurrentCulture values affects the .NET side of localization but doesn't necessarily touch the native platform. So if you had, say, a RESX file with the fr language, that would be set and rendered, but these controls would not get touched because the underlying platform still treats the overall system as running in whatever local you have.

For example, on iOS/Mac Catalyst, you need to declare the language in your Info.plist (The documentation refers to Xamarin.iOS but it also applies to MAUI for iOS and Catalyst),

スクリーンショット 2023-06-21 18 05 55

If you set those values, then it will appear in the local you want, regardless of what's on the system. This is also how you can allow users in the iOS setting app to change the application's language.

In the case of iOS/Catalyst, AFAIK, there's no way to programmatically control what local the controls use without at least setting up the info.plist (CC @rolfbjarne is that right?)

IMO, this isn't a MAUI bug persay, we need documentation on the proper way to localize a MAUI app, just setting the CurrentCulture in dotnet isn't enough.

@Redth @davidbritch does that sound right?

saiganesh-sakthivel commented 1 year ago

Hi @drasticactions,

Any proper guidelines is have to achieve the MAUI DatePicker calendar view localization for Windows, UWP, android, iOS and MacOS.

rolfbjarne commented 1 year ago

In the case of iOS/Catalyst, AFAIK, there's no way to programmatically control what local the controls use without at least setting up the info.plist (CC @rolfbjarne is that right?)

Correct, and also note that the correct way to change the selected locale of the app is to change it in the Settings app (see https://developer.apple.com/news/?id=u2cfuj88).

davidbritch commented 1 year ago

@Redth @davidbritch does that sound right?

Yes. IIRC (and my memory's a bit fuzzy on this), I had some success in updating the approach to use Microsoft.Extensions.Localization.

But yes, the doc still needs writing.

Legends commented 6 months ago

On windows in Package.appxmanifest:

If I only have <Resource Language="x-generate" /> the datepicker will use the current os language. If I specify one language, it will use the language specified. If I have multiple languages specified, it will take the first in order.

<Resources>
  <!--<Resource Language="x-generate" />-->
  <Resource Language="de-DE"/>
  <!--<Resource Language="en-US"/>-->
  <!--<Resource Language="nl"/>-->
</Resources>

So, these settings determines the language of the datepicker. How can I change the language of the datepicker during runtime?

This doesn't work:

var userLanguages = new List<string>() { iso };
DatePickerHandler.Mapper.AppendToMapping("FixDatepickerLanguage", (handler, view) =>
{
    handler.PlatformView.Language = twoLetterIso;
});
Legends commented 6 months ago

@saiganesh-sakthivel This is not specific to the DatePicker control, it's how you're setting the language in code.

  public App()
  {
      InitializeComponent();
        var culture = new CultureInfo("fr-FR"); // Replace with your desired culture or language code
        CultureInfo.DefaultThreadCurrentCulture = culture;
        CultureInfo.DefaultThreadCurrentUICulture = culture;
        MainPage = new AppShell();
  }

Setting the CurrentCulture values affects the .NET side of localization but doesn't necessarily touch the native platform. So if you had, say, a RESX file with the fr language, that would be set and rendered, but these controls would not get touched because the underlying platform still treats the overall system as running in whatever local you have.

For example, on iOS/Mac Catalyst, you need to declare the language in your Info.plist (The documentation refers to Xamarin.iOS but it also applies to MAUI for iOS and Catalyst),

スクリーンショット 2023-06-21 18 05 55

If you set those values, then it will appear in the local you want, regardless of what's on the system. This is also how you can allow users in the iOS setting app to change the application's language.

In the case of iOS/Catalyst, AFAIK, there's no way to programmatically control what local the controls use without at least setting up the info.plist (CC @rolfbjarne is that right?)

IMO, this isn't a MAUI bug persay, we need documentation on the proper way to localize a MAUI app, just setting the CurrentCulture in dotnet isn't enough.

@Redth @davidbritch does that sound right?

@Redth @davidbritch @drasticactions :

How can I localize the datepicker on Android?

If I set the folwoing code in MainApplication.cs : this.Resources.Configuration.Locales = new Android.OS.LocaleList(new Java.Util.Locale("fr-FR"));

then, I only get the header in French and the rest stays in German:

Screenshot_2024-01-24-11-16-37-999_com companyname mauilocalizationsample

Zhanglirong-Winnie commented 6 months ago

Verified this issue with Visual Studio Enterprise 17.9.0 Preview 4. Can repro on android platforms with sample project. It's no longer repro on iOS and Windows platforms. https://github.com/dotnet/maui/files/11798627/MauiApp1.zip