简体   繁体   中英

.Net 5 Japanese culture month abbreviation

I'm using .Net 5.0.403 to format a Japanese date, and I'm getting a different value on two hosts. I'm running the following code:

using System;
using System.Globalization;

var locale = new CultureInfo("ja-JP");
CultureInfo.CurrentCulture = locale;
var formatted = DateTime.Now.ToString("dd MMM yyyy H:mm");
Console.WriteLine(formatted);

Console.WriteLine("Month abbreviations:");
foreach (var month in locale.DateTimeFormat.AbbreviatedMonthNames)
    Console.WriteLine("* {0}", month);

Which locally gives the output:

09 11月 2021 16:21
Month abbreviations:
* 1月
* 2月
* 3月
* 4月
* 5月
* 6月
* 7月
* 8月
* 9月
* 10月
* 11月
* 12月
*

However, when running on a build server with Windows 2019, I get the following output:

09 11 2021 16:22
Month abbreviations:
* 1
* 2
* 3
* 4
* 5
* 6
* 7
* 8
* 9
* 10
* 11
* 12
* 

It seems that the MMM format string corresponds to a different value on the build server. Are there any configuration options that can be set on a host to control this?

This was caused by the inclusion of ICU lib in .net 5. It relied on the version available on the host machine, and this could vary between environments. This is detailed in this bug https://github.com/dotnet/runtime/issues/60845 . The solution was to embed a fixed version of ICU libs for use in all environments by adding the following to the csproj file of the assembly that is executing:

  <ItemGroup>
        <RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68.2.0.6" />
        <PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.6" />
  </ItemGroup>

This is referred to app-local ICU, and is further documented here: https://docs.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#app-local-icu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM