You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.5 KiB
52 lines
1.5 KiB
using System.Collections.Generic; |
|
using System.Collections.Immutable; |
|
using System.Diagnostics.CodeAnalysis; |
|
using System.Globalization; |
|
|
|
namespace StabilityMatrix.Avalonia.Languages; |
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] |
|
public static class Cultures |
|
{ |
|
public static CultureInfo Default { get; } = new("en-US"); |
|
|
|
public static CultureInfo Current => Resources.Culture; |
|
|
|
public static readonly Dictionary<string, CultureInfo> SupportedCulturesByCode = |
|
new Dictionary<string, CultureInfo> |
|
{ |
|
["en-US"] = Default, |
|
["ja-JP"] = new("ja-JP") |
|
}; |
|
|
|
public static IReadOnlyList<CultureInfo> SupportedCultures |
|
=> SupportedCulturesByCode.Values.ToImmutableList(); |
|
|
|
public static CultureInfo GetSupportedCultureOrDefault(string? cultureCode) |
|
{ |
|
if (cultureCode is null |
|
|| !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture)) |
|
{ |
|
return Default; |
|
} |
|
|
|
return culture; |
|
} |
|
|
|
public static bool TrySetSupportedCulture(string? cultureCode) |
|
{ |
|
if (cultureCode is null |
|
|| !SupportedCulturesByCode.TryGetValue(cultureCode, out var culture)) |
|
{ |
|
return false; |
|
} |
|
|
|
Resources.Culture = culture; |
|
return true; |
|
} |
|
|
|
public static bool TrySetSupportedCulture(CultureInfo? cultureInfo) |
|
{ |
|
return cultureInfo is not null && TrySetSupportedCulture(cultureInfo.Name); |
|
} |
|
}
|
|
|