Skip to content

Commit

Permalink
[browser][non-icu] HybridGlobalization calendar data (#89255)
Browse files Browse the repository at this point in the history
* Calendar WIP.

* Genitive months.

* Short pattern.

* NativeCalendarName does not exist on WebAPI - use EnglishCalendarName.

* Fix failures in Locales caused by calendars data removal.

* Fix to previous commit.

* Whitespace.

* Implemented Eras.

* Populate NativeName with EnglishName.

* Fix tests.

* Typos + comments removal + block failing test.

* AM/PM designators are not in ICU anymore.

* ShortTimePattern is ready.

* Fix some failing tests.

* LongTimePatterns is ready.

* Ask about all culture info from JS at once.

* Fix remaining tests.

* Calendar Globalization tests fixed.

* Adding test files to projects.

* Build fix.

* Fix the fix.

* .

* Fix tests.

* Load locale info on request + fix Browser scenario tests.

* Fix test on v8.

* Forgotten clean-up.

* Small cleanup in ts.

* V8 11 is behaves more like browser.

* Fixed skipped failing tests.

* Syntax

* V8 returns the same as Browser.

* This API is not affected, no need to test. + Add documentation.

* Revert unintentional change.

* Feedback.

* @radical's feedback + fix after removing unnecessary set.

* Fix.

* Feedback.

* Fix tests.
  • Loading branch information
ilonatommy authored Aug 7, 2023
1 parent 6029c30 commit a9363bf
Show file tree
Hide file tree
Showing 45 changed files with 4,861 additions and 25 deletions.
55 changes: 55 additions & 0 deletions docs/design/features/globalization-hybrid-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,61 @@ Using `IgnoreNonSpace` for these two with `HybridGlobalization` off, also return
new CultureInfo("de-DE").CompareInfo.IndexOf("strasse", "stra\u00DFe", 0, CompareOptions.IgnoreNonSpace); // 0 or -1
```

**Calandars**

Affected public APIs:
- DateTimeFormatInfo.AbbreviatedDayNames
- DateTimeFormatInfo.GetAbbreviatedDayName()
- DateTimeFormatInfo.AbbreviatedMonthGenitiveNames
- DateTimeFormatInfo.AbbreviatedMonthNames
- DateTimeFormatInfo.GetAbbreviatedMonthName()
- DateTimeFormatInfo.AMDesignator
- DateTimeFormatInfo.CalendarWeekRule
- DateTimeFormatInfo.DayNames
- DateTimeFormatInfo.GetDayName
- DateTimeFormatInfo.GetAbbreviatedEraName()
- DateTimeFormatInfo.GetEraName()
- DateTimeFormatInfo.FirstDayOfWeek
- DateTimeFormatInfo.FullDateTimePattern
- DateTimeFormatInfo.LongDatePattern
- DateTimeFormatInfo.LongTimePattern
- DateTimeFormatInfo.MonthDayPattern
- DateTimeFormatInfo.MonthGenitiveNames
- DateTimeFormatInfo.MonthNames
- DateTimeFormatInfo.GetMonthName()
- DateTimeFormatInfo.NativeCalendarName
- DateTimeFormatInfo.PMDesignator
- DateTimeFormatInfo.ShortDatePattern
- DateTimeFormatInfo.ShortestDayNames
- DateTimeFormatInfo.GetShortestDayName()
- DateTimeFormatInfo.ShortTimePattern
- DateTimeFormatInfo.YearMonthPattern


The Hybrid responses may differ because they use Web API functions. To better ilustrate the mechanism we provide an example for each endpoint. All exceptions cannot be listed, for reference check the response of specific version of Web API on your host.
| **API** | **Functions used** | **Example of difference for locale** | **non-Hybrid** | **Hybrid** |
|:-----------------------------:|:----------------------------------------------------------------------------------------------------------------------------------:|:------------------------------------:|:------------------:|:-----------------:|
| AbbreviatedDayNames | `Date.prototype.toLocaleDateString(locale, { weekday: "short" })` | en-CA | Sun. | Sun |
| AbbreviatedMonthGenitiveNames | `Date.prototype.toLocaleDateString(locale, { month: "short", day: "numeric"})` | kn-IN | ಆಗ | ಆಗಸ್ಟ್ |
| AbbreviatedMonthNames | `Date.prototype.toLocaleDateString(locale, { month: "short" })` | lt-LT | saus. | 01 |
| AMDesignator | `Date.prototype.toLocaleTimeString(locale, { hourCycle: "h12"})`; `Date.prototype.toLocaleTimeString(locale, { hourCycle: "h24"})` | sr-Cyrl-RS | пре подне | AM |
| CalendarWeekRule | `Intl.Locale.prototype.getWeekInfo().minimalDay` | none | - | - |
| DayNames | `Date.prototype.toLocaleDateString(locale, { weekday: "long" })` | none | - | - |
| GetAbbreviatedEraName() | `Date.prototype.toLocaleDateString(locale, { era: "narrow" })` | bn-IN | খৃষ্টাব্দ | খ্রিঃ |
| GetEraName() | `Date.prototype.toLocaleDateString(locale, { era: "short" })` | vi-VI | sau CN | CN |
| FirstDayOfWeek | `Intl.Locale.prototype.getWeekInfo().firstDay` | zn-CN | Sunday | Monday |
| FullDateTimePattern | `LongDatePattern` and `LongTimePattern` | - | | |
| LongDatePattern | `Intl.DateTimeFormat(locale, { weekday: "long", year: "numeric", month: "long", day: "numeric"}).format(date)` | en-BW | dddd, dd MMMM yyyy | dddd, d MMMM yyyy |
| LongTimePattern | `Intl.DateTimeFormat(locale, { timeStyle: "medium" })` | zn-CN | tth:mm:ss | HH:mm:ss |
| MonthDayPattern | `Date.prototype.toLocaleDateString(locale, { month: "long", day: "numeric"})` | en-PH | d MMMM | MMMM d |
| MonthGenitiveNames | `Date.prototype.toLocaleDateString(locale, { month: "long", day: "numeric"})` | ca-AD | de gener | gener |
| MonthNames | `Date.prototype.toLocaleDateString(locale, { month: "long" })` | el-GR | Ιανουαρίου | Ιανουάριος |
| NativeCalendarName | `Intl.Locale.prototype.getCalendars()` | for all locales it has English names | Gregorian Calendar | gregory |
| PMDesignator | `Date.prototype.toLocaleTimeString(locale, { hourCycle: "h12"})`; `Date.prototype.toLocaleTimeString(locale, { hourCycle: "h24"})` | mr-IN | म.उ. | PM |
| ShortDatePattern | `Date.prototype.toLocaleDateString(locale, {dateStyle: "short"})` | en-CH | dd.MM.yyyy | dd/MM/yyyy |
| ShortestDayNames | `Date.prototype.toLocaleDateString(locale, { weekday: "narrow" })` | none | - | - |
| ShortTimePattern | `Intl.DateTimeFormat(locale, { timeStyle: "medium" })` | bg-BG | HH:mm | H:mm |
| YearMonthPattern | `Date.prototype.toLocaleDateString(locale, { year: "numeric", month: "long" })` | ar-SA | MMMM yyyy | MMMM yyyy g |

### OSX

Expand Down
14 changes: 14 additions & 0 deletions src/libraries/Common/src/Interop/Browser/Interop.Calendar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.Runtime.CompilerServices;

internal static partial class Interop
{
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetCalendarInfo(in string culture, CalendarId calendarId, char* buffer, int bufferLength, out int exceptionalResult, out object result);
}
}
17 changes: 17 additions & 0 deletions src/libraries/Common/src/Interop/Browser/Interop.Locale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

internal static partial class Interop
{
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetCultureInfo(in string culture, char* buffer, int bufferLength, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetFirstDayOfWeek(in string culture, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetFirstWeekOfYear(in string culture, out int exceptionalResult, out object result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public static string GetDistroVersionString()
public static bool IsNotHybridGlobalizationOnBrowser => !IsHybridGlobalizationOnBrowser;
public static bool IsNotInvariantGlobalization => !IsInvariantGlobalization;
public static bool IsIcuGlobalization => ICUVersion > new Version(0, 0, 0, 0);
public static bool IsIcuGlobalizationAndNotHybridOnBrowser => IsIcuGlobalization && IsNotHybridGlobalizationOnBrowser;
public static bool IsNlsGlobalization => IsNotInvariantGlobalization && !IsIcuGlobalization;

public static bool IsSubstAvailable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-browser</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TestRuntime>true</TestRuntime>
<HybridGlobalization>true</HybridGlobalization>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\System\Globalization\CalendarTestBase.cs" />
<Compile Include="..\System\Globalization\ChineseLunisolarCalendarTests.cs" />
<Compile Include="..\System\Globalization\EastAsianLunisolarCalendarTestBase.cs" />
<Compile Include="..\System\Globalization\GregorianCalendarTests.cs" />
<Compile Include="..\System\Globalization\HebrewCalendarTests.cs" />
<Compile Include="..\System\Globalization\HijriCalendarTests.cs" />
<Compile Include="..\System\Globalization\JapaneseCalendarTests.cs" />
<Compile Include="..\System\Globalization\JapaneseLunisolarCalendarTests.cs" />
<Compile Include="..\System\Globalization\JulianCalendarTests.cs" />
<Compile Include="..\System\Globalization\KoreanCalendarTests.cs" />
<Compile Include="..\System\Globalization\KoreanLunisolarCalendarTests.cs" />
<Compile Include="..\System\Globalization\PersianCalendarTests.cs" />
<Compile Include="..\System\Globalization\TaiwanCalendarTests.cs" />
<Compile Include="..\System\Globalization\TaiwanLunisolarCalendarTests.cs" />
<Compile Include="..\System\Globalization\ThaiBuddhistCalendarTests.cs" />
<Compile Include="..\System\Globalization\UmAlQuraCalendarTests.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarAddMonths.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarAddYears.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetDayOfMonth.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetDayOfWeek.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetDayOfYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetDaysInMonth.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetDaysInYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetEra.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetMonth.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetLeapMonth.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetMonthsInYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetWeekOfYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarGetYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarIsLeapDay.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarIsLeapMonth.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarIsLeapYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarTests.Utilities.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarToDateTime.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarToFourDigitYear.cs" />
<Compile Include="..\GregorianCalendar\GregorianCalendarTwoDigitYearMax.cs" />
<Compile Include="..\ISOWeek\ISOWeekTests.cs" />
<Compile Include="..\JapaneseCalendar\JapaneseCalendarAddMonths.cs" />
<Compile Include="..\JapaneseCalendar\JapaneseCalendarToFourDigitYear.cs" />
<Compile Include="..\JapaneseCalendar\JapaneseCalendarTwoDigitYearMax.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarToDateTime.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarAddMonths.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarAddYears.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetDayOfMonth.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetDayOfWeek.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetDayOfYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetDaysInMonth.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetDaysInYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetEra.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetMonth.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetMonthsInYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetWeekOfYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarGetYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarIsLeapDay.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarIsLeapMonth.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarIsLeapYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarToFourDigitYear.cs" />
<Compile Include="..\KoreanCalendar\KoreanCalendarTwoDigitYearMax.cs" />
<Compile Include="..\Misc\MiscCalendars.cs" />
<Compile Include="..\Misc\Calendars.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarUtilities.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarAddMonths.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarAddYears.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarDaysAndMonths.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetDayOfMonth.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetDayOfWeek.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetDayOfYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetDaysInMonth.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetDaysInYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetEra.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetMonth.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetMonthsInYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetWeekOfYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarGetYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarIsLeapDay.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarIsLeapMonth.cs" />
<Compile Include="..\TaiwanCalendar\TaiWanCalendarIsLeapYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarToDateTime.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarToFourDigitYear.cs" />
<Compile Include="..\TaiwanCalendar\TaiwanCalendarTwoDigitYearMax.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarAddMonths.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarAddYears.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetDayOfMonth.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetDayOfWeek.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetDayOfYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetDaysInMonth.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetDaysInYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetEra.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetMonth.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetMonthsInYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetWeekOfYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarGetYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarIsLeapDay.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarIsLeapMonth.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarIsLeapYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarToDateTime.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarToFourDigitYear.cs" />
<Compile Include="..\ThaiBuddhistCalendar\ThaiBuddhistCalendarTwoDigitYearMax.cs" />
<Compile Include="$(CommonTestPath)System\RandomDataGenerator.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ public void GetEra_Invalid_ThrowsArgumentOutOfRangeException()
Assert.All(DateTime_TestData(calendar), dt =>
{
// JapaneseCalendar throws on ICU, but not on NLS
if ((calendar is JapaneseCalendar && PlatformDetection.IsNlsGlobalization) || calendar is HebrewCalendar || calendar is TaiwanLunisolarCalendar || calendar is JapaneseLunisolarCalendar)
if ((calendar is JapaneseCalendar && PlatformDetection.IsNlsGlobalization) ||
calendar is HebrewCalendar ||
calendar is TaiwanLunisolarCalendar ||
calendar is JapaneseLunisolarCalendar)
{
calendar.GetEra(dt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public void TestCreationWithTemporaryLCID(int lcid)
[InlineData("de-DE-u-co-phonebk-t-xx", "de-DE-t-xx", "de-DE-t-xx_phoneboo")]
[InlineData("de-DE-u-co-phonebk-t-xx-u-yy", "de-DE-t-xx-u-yy", "de-DE-t-xx-u-yy_phoneboo")]
[InlineData("de-DE", "de-DE", "de-DE")]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization))]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalizationAndNotHybridOnBrowser))]
public void TestCreationWithMangledSortName(string cultureName, string expectedCultureName, string expectedSortName)
{
CultureInfo ci = CultureInfo.GetCultureInfo(cultureName);
Expand All @@ -457,7 +457,7 @@ public void TestCreationWithMangledSortName(string cultureName, string expectedC
[InlineData("qps-plocm", "qps-PLOCM")] // ICU normalize this name to "qps--plocm" which we normalize it back to "qps-plocm"
[InlineData("zh_CN", "zh_cn")]
[InlineData("km_KH", "km_kh")]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalization), nameof(PlatformDetection.IsNotWindowsServerCore))]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsIcuGlobalizationAndNotHybridOnBrowser), nameof(PlatformDetection.IsNotWindowsServerCore))]
public void TestCreationWithICUNormalizedNames(string cultureName, string expectedCultureName)
{
CultureInfo ci = CultureInfo.GetCultureInfo(cultureName);
Expand Down
Loading

0 comments on commit a9363bf

Please sign in to comment.