-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Fallback to invariant culture if cultures are not found (#1266)
Fixes #1238 Applications crashed when running on Linux or Raspberry PI systems if .NET cultures were not installed. Specifically, `UnitAbbreviationsCache.Default` threw an exception trying to instantiate the fallback `CultureInfo` with `en-US`. ### Changes - Change fallback culture to `InvariantCulture` - Add `CultureHelper.GetCultureOrInvariant()` to handle `CultureNotFoundException` - Change `UnitInfo` to map invariant culture to `en-US` localization
- Loading branch information
1 parent
6bd05bf
commit 439c143
Showing
5 changed files
with
82 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Globalization; | ||
|
||
namespace UnitsNet.InternalHelpers; | ||
|
||
/// <summary> | ||
/// Helper class for <see cref="CultureInfo"/> and related operations. | ||
/// </summary> | ||
internal static class CultureHelper | ||
{ | ||
private static readonly ConcurrentDictionary<string, CultureInfo> CultureCache = new(); | ||
|
||
/// <summary> | ||
/// Attempts to get the culture by name, with fallback to invariant culture if not found.<br/> | ||
/// <br/> | ||
/// This is particularly useful for Linux and Raspberry PI environments, where cultures may not always be installed. | ||
/// To simulate the behavior, set environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='1' when running the application. | ||
/// </summary> | ||
/// <param name="cultureName">The culture name.</param> | ||
/// <returns><see cref="CultureInfo.CurrentCulture"/> if given <c>null</c>, or the culture with the given name if the culture is available, otherwise <see cref="CultureInfo.InvariantCulture"/>.</returns> | ||
internal static CultureInfo GetCultureOrInvariant(string? cultureName) | ||
{ | ||
if (cultureName is null) return CultureInfo.CurrentCulture; | ||
|
||
try | ||
{ | ||
// Use cache to avoid exception and diagnostic log events every time. | ||
return CultureCache.GetOrAdd(cultureName, CultureInfo.GetCultureInfo); | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
Console.Error.WriteLine($"Failed to get culture '{cultureName}', falling back to invariant culture."); | ||
System.Diagnostics.Debug.WriteLine($"Failed to get culture '{cultureName}', falling back to invariant culture."); | ||
|
||
// Cache it, to avoid exception next time. | ||
CultureCache.TryAdd(cultureName, CultureInfo.InvariantCulture); | ||
|
||
return CultureInfo.InvariantCulture; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters