Skip to content

Instantly share code, notes, and snippets.

@0xced
Created August 12, 2022 20:55
Show Gist options
  • Save 0xced/573db91e25f8124c8a3abbb3f230f636 to your computer and use it in GitHub Desktop.
Save 0xced/573db91e25f8124c8a3abbb3f230f636 to your computer and use it in GitHub Desktop.
Playing around with App-local ICU
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<DebugType>embedded</DebugType>
<PublishSingleFile>true</PublishSingleFile>
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
<SelfContained>false</SelfContained>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.9" />
</ItemGroup>
</Project>
using System;
using System.Globalization;
using System.IO;
using System.Linq;
var output = args.Length == 1 ? new StreamWriter(args[0]) : Console.Out;
// Alternatively, could also use <RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68" /> in csproj
// See https://docs.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#app-local-icu
output.WriteLine($"DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU = {Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU")}");
output.WriteLine($"Is using ICU? = {IsUsingIcu()}");
foreach (var name in new[] { "de-CH", "en-CH", "fr-CH", "it-CH" })
{
output.WriteLine($"Culture: {name}");
{
var culture = new CultureInfo(name, useUserOverride: false);
output.WriteLine($" NumberDecimalSeparator = {UnicodeDescription(culture.NumberFormat.NumberDecimalSeparator)} | NumberGroupSeparator = {UnicodeDescription(culture.NumberFormat.NumberGroupSeparator)}");
output.WriteLine($" CurrencyDecimalSeparator = {UnicodeDescription(culture.NumberFormat.CurrencyDecimalSeparator)} | CurrencyGroupSeparator = {UnicodeDescription(culture.NumberFormat.CurrencyGroupSeparator)}");
output.WriteLine($" ShortDatePattern = {culture.DateTimeFormat.ShortDatePattern}");
}
}
output.Flush();
static string UnicodeDescription(string value)
{
return string.Join(" ", value.EnumerateRunes().Select(r => $"{r} (U+{r.Value:X4})"));
}
// From https://docs.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#determine-if-your-app-is-using-icu
static bool IsUsingIcu()
{
var sortVersion = CultureInfo.InvariantCulture.CompareInfo.Version;
var bytes = sortVersion.SortId.ToByteArray();
var version = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
return version != 0 && version == sortVersion.FullVersion;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment