lightweight variant (#1)
Reviewed-on: #1 Co-authored-by: Aleksandr Neychev <alexnejchev73@gmail.com>
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user