using System.Globalization;
namespace CursorLang.Core.Models;
///
/// A keyboard layout in a form convenient for display.
///
/// The locale identifier (the low word of HKL).
/// A short name for the popup at the cursor, "RU" for instance.
/// The full name, "RU — русский (Россия)" for instance.
public sealed record KeyboardLayout(int LocaleId, string ShortName, string DisplayName)
{
///
/// Builds the model from a locale identifier. Unknown locales are not an
/// error: for them we show the identifier itself.
///
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;
}
}
}