| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | |
| | | 7 | | namespace System.Globalization |
| | | 8 | | { |
| | | 9 | | internal static partial class GlobalizationMode |
| | | 10 | | { |
| | | 11 | | // Split from GlobalizationMode so the whole class can be trimmed when Invariant=true. Trimming tests |
| | | 12 | | // validate this implementation detail. |
| | | 13 | | private static partial class Settings |
| | | 14 | | { |
| | 0 | 15 | | internal static bool Invariant { get; } = AppContextConfigHelper.GetBooleanConfig("System.Globalization.Inva |
| | | 16 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 17 | | internal static bool Hybrid { get; } = true; |
| | | 18 | | #endif |
| | 0 | 19 | | internal static bool PredefinedCulturesOnly { get; } = AppContextConfigHelper.GetBooleanConfig("System.Globa |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | // Note: Invariant=true and Invariant=false are substituted at different levels in the ILLink.Substitutions file |
| | | 23 | | // This allows for the whole Settings nested class to be trimmed when Invariant=true, and allows for the Setting |
| | | 24 | | // static cctor (on Unix) to be preserved when Invariant=false. |
| | 0 | 25 | | internal static bool Invariant => Settings.Invariant; |
| | | 26 | | |
| | | 27 | | // same as GlobalizationMode.Invariant but doesn't trigger ICU load in GlobalizationMode.Settings.cctor |
| | | 28 | | // during runtime startup on Browser platform |
| | | 29 | | internal static bool InvariantNoLoad |
| | | 30 | | { |
| | | 31 | | get |
| | | 32 | | { |
| | | 33 | | #if TARGET_BROWSER |
| | | 34 | | return AppContextConfigHelper.GetBooleanConfig("System.Globalization.Invariant", "DOTNET_SYSTEM_GLOBALIZ |
| | | 35 | | #else |
| | 0 | 36 | | return Settings.Invariant; |
| | | 37 | | #endif |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 42 | | internal static bool Hybrid => Settings.Hybrid; |
| | | 43 | | #endif |
| | 0 | 44 | | internal static bool PredefinedCulturesOnly => Settings.PredefinedCulturesOnly; |
| | | 45 | | |
| | | 46 | | private static bool TryGetAppLocalIcuSwitchValue([NotNullWhen(true)] out string? value) => |
| | 0 | 47 | | TryGetStringValue("System.Globalization.AppLocalIcu", "DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU", out value); |
| | | 48 | | private static bool TryGetStringValue(string switchName, string envVariable, [NotNullWhen(true)] out string? val |
| | | 49 | | { |
| | 0 | 50 | | value = AppContext.GetData(switchName) as string; |
| | 0 | 51 | | if (string.IsNullOrEmpty(value)) |
| | | 52 | | { |
| | 0 | 53 | | value = Environment.GetEnvironmentVariable(envVariable); |
| | 0 | 54 | | if (string.IsNullOrEmpty(value)) |
| | | 55 | | { |
| | 0 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private static void LoadAppLocalIcu(string icuSuffixAndVersion) |
| | | 64 | | { |
| | | 65 | | ReadOnlySpan<char> version; |
| | 0 | 66 | | ReadOnlySpan<char> icuSuffix = default; |
| | | 67 | | |
| | | 68 | | // Custom built ICU can have a suffix on the name, i.e: libicuucmyapp.so.67.1 |
| | | 69 | | // So users would set the runtime switch as: myapp:67.1 |
| | 0 | 70 | | int indexOfSeparator = icuSuffixAndVersion.IndexOf(':'); |
| | 0 | 71 | | if (indexOfSeparator >= 0) |
| | | 72 | | { |
| | 0 | 73 | | icuSuffix = icuSuffixAndVersion.AsSpan(0, indexOfSeparator); |
| | 0 | 74 | | version = icuSuffixAndVersion.AsSpan(icuSuffix.Length + 1); |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 0 | 78 | | version = icuSuffixAndVersion; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | LoadAppLocalIcuCore(version, icuSuffix); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private static string CreateLibraryName(ReadOnlySpan<char> baseName, ReadOnlySpan<char> suffix, ReadOnlySpan<cha |
| | 0 | 85 | | versionAtEnd ? |
| | 0 | 86 | | string.Concat(baseName, suffix, extension, version) : |
| | 0 | 87 | | string.Concat(baseName, suffix, version, extension); |
| | | 88 | | |
| | | 89 | | private static IntPtr LoadLibrary(string library, bool failOnLoadFailure) |
| | | 90 | | { |
| | 0 | 91 | | if (!NativeLibrary.TryLoad(library, typeof(object).Assembly, DllImportSearchPath.ApplicationDirectory | DllI |
| | | 92 | | { |
| | 0 | 93 | | Environment.FailFast($"Failed to load app-local ICU: {library}"); |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return lib; |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |