42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Globalization;
|
|
|
|
namespace CursorLang.Core.Models;
|
|
|
|
/// <summary>
|
|
/// A keyboard layout in a form convenient for display.
|
|
/// </summary>
|
|
/// <param name="LocaleId">The locale identifier (the low word of HKL).</param>
|
|
/// <param name="ShortName">A short name for the popup at the cursor, "RU" for instance.</param>
|
|
/// <param name="DisplayName">The full name, "RU — русский (Россия)" for instance.</param>
|
|
public sealed record KeyboardLayout(int LocaleId, string ShortName, string DisplayName)
|
|
{
|
|
/// <summary>
|
|
/// Builds the model from a locale identifier. Unknown locales are not an
|
|
/// error: for them we show the identifier itself.
|
|
/// </summary>
|
|
public static KeyboardLayout FromLocaleId(int localeId)
|
|
{
|
|
CultureInfo? culture = TryGetCulture(localeId);
|
|
if (culture is null)
|
|
{
|
|
string fallback = $"0x{localeId:X4}";
|
|
return new KeyboardLayout(localeId, fallback, fallback);
|
|
}
|
|
|
|
string shortName = culture.TwoLetterISOLanguageName.ToUpperInvariant();
|
|
return new KeyboardLayout(localeId, shortName, $"{shortName} — {culture.NativeName}");
|
|
}
|
|
|
|
private static CultureInfo? TryGetCulture(int localeId)
|
|
{
|
|
try
|
|
{
|
|
return new CultureInfo(localeId);
|
|
}
|
|
catch (CultureNotFoundException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|