Skip to content

Commit

Permalink
fix: Adjust support for InvariantCulture mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 26, 2024
1 parent 58e477b commit 39931e8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/Uno.UI/UI/Xaml/Application.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.UI.Xaml.Media;
using Uno.UI.Dispatching;
using Uno.UI.Xaml.Core;
using Windows.Globalization;

namespace Microsoft.UI.Xaml
{
Expand Down Expand Up @@ -58,18 +59,29 @@ private void SetCurrentLanguage()
if (CultureInfo.CurrentUICulture.IetfLanguageTag == "" &&
CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "iv")
{
try
if (!ApplicationLanguages.InvariantCulture)
{
// Fallback to English
var cultureInfo = CultureInfo.CreateSpecificCulture("en");
CultureInfo.CurrentUICulture = cultureInfo;
CultureInfo.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

try
{
// Fallback to English
var cultureInfo = CultureInfo.CreateSpecificCulture("en");
CultureInfo.CurrentUICulture = cultureInfo;
CultureInfo.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
}
catch (Exception ex)
{
this.Log().Error($"Failed to set default culture", ex);
}
}
catch (Exception ex)
else
{
this.Log().Error($"Failed to set default culture", ex);
if (typeof(ApplicationLanguages).Log().IsEnabled(LogLevel.Debug))
{
typeof(ApplicationLanguages).Log().Debug("InvariantCulture mode is enabled");
}
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/Uno.UWP/Globalization/ApplicationLanguages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static partial class ApplicationLanguages
{
private static string? _primaryLanguageOverride = string.Empty;

internal readonly static bool InvariantCulture = GetBooleanConfig("System.Globalization.Invariant", "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");


#if !IS_UNIT_TESTS
private const string PrimaryLanguageOverrideSettingKey = "__Uno.PrimaryLanguageOverride";
#endif
Expand Down Expand Up @@ -106,6 +109,13 @@ static void setCulture(string cultureId)
#if !__IOS__
private static string[] GetManifestLanguages()
{
if (InvariantCulture)
{
// The invariant culture can be created with `new CultureInfo("")`.
// We return this as part of the supported languages.
return [""];
}

string AdjustCultureName(string? name)
=> string.IsNullOrEmpty(name) ? "en-US" : name;

Expand Down Expand Up @@ -141,6 +151,15 @@ private static string[] GetManifestLanguages()
[MemberNotNull(nameof(Languages))]
private static void ApplyLanguages()
{
if (InvariantCulture)
{
// The invariant culture can be created with `new CultureInfo("")`.
// We return this as part of the supported languages
Languages = [""];

return;
}

#if false
Languages = GlobalizationPreferences.Languages
.Cast<string?>()
Expand Down Expand Up @@ -211,6 +230,31 @@ private static CultureInfo CreateCulture(string cultureId)
}
}

internal static bool GetBooleanConfig(string switchName, bool defaultValue) =>
AppContext.TryGetSwitch(switchName, out bool value) ? value : defaultValue;

internal static bool GetBooleanConfig(string switchName, string envVariable, bool defaultValue = false)
{
string? str = Environment.GetEnvironmentVariable(envVariable);

if (str != null)
{
if (str == "1" || str.Equals("true", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (str == "0" || str.Equals("false", StringComparison.OrdinalIgnoreCase))
{
return false;
}
}

return GetBooleanConfig(switchName, defaultValue);
}


[GeneratedRegex(@"(?<lang>[a-z]{2,8})(?:(?:\-(?<script>[a-zA-Z]+))?\-(?<reg>[A-Z]+))?", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.CultureInvariant)]
private static partial Regex CultureRegex();


}

0 comments on commit 39931e8

Please sign in to comment.