Skip to content

Instantly share code, notes, and snippets.

@Sl4vP0weR
Created August 12, 2023 14:10
Show Gist options
  • Save Sl4vP0weR/12bfdb01ced60014418ad281c537389a to your computer and use it in GitHub Desktop.
Save Sl4vP0weR/12bfdb01ced60014418ad281c537389a to your computer and use it in GitHub Desktop.
Language to culture implementation.
using System;
using System.Globalization;
public enum Language : int
{
Arabic = 1,
German = 7,
English = 9,
Spanish = 10,
French = 12,
Japanese = 17,
Portuguese = 22,
Russian = 25,
Hindi = 57,
Chinese = 30724,
}
public static class LanguageExtensions
{
public const Language DefaultLanguage = Language.English;
/// <summary>
/// Retrieves <see cref="Language" /> from <paramref name="culture"/>.
/// </summary>
/// <param name="culture">Culture using language.</param>
/// <param name="fallback">Fallback language if original is not defined.</param>
public static Language AsLanguage(this CultureInfo culture, Language fallback = DefaultLanguage) =>
culture.GetRootParent().LCID.AsLanguage().Valid(fallback);
/// <summary>
/// Retrieves <see cref="CultureInfo"/> from the operating system by <paramref name="language"/>.
/// </summary>
/// <param name="language">Language of culture.</param>
/// <exception cref="CultureNotFoundException">Culture is not supported on current machine.</exception>
public static CultureInfo AsCulture(this Language language) =>
CultureInfo.GetCultureInfo(language.AsCode());
/// <summary>
/// Retrieves <see cref="CultureInfo"/> from the operating system by <paramref name="language"/>
/// or returns <paramref name="fallback"/> if original culture is not supported.
/// </summary>
/// <param name="language">Language of culture.</param>
/// <param name="fallback">Fallback culture if original culture is not supported.</param>
public static CultureInfo AsCulture(this Language language, CultureInfo fallback)
{
try
{
return language.AsCulture();
}
catch
{
return fallback;
}
}
/// <summary>
/// Converts <paramref name="language"/> to corresponding <see cref="CultureInfo.LCID"/>.
/// </summary>
/// <param name="language">Language to convert.</param>
public static int AsCode(this Language language) =>
(int)language;
/// <summary>
/// Converts <paramref name="languageCode"/> to corresponding <see cref="Language"/>.
/// </summary>
/// <param name="languageCode">LCID to convert.</param>
public static Language AsLanguage(this int languageCode) =>
(Language)languageCode;
/// <summary>
/// Returns <paramref name="language"/> if it is valid, otherwise <paramref name="fallback"/>.
/// </summary>
/// <param name="language">Language to validate.</param>
/// <param name="fallback">Fallback language.</param>
public static Language Valid(this Language language, Language fallback = DefaultLanguage) =>
Enum.IsDefined(typeof(Language), language) ? language : fallback;
public static CultureInfo GetRootParent(this CultureInfo culture)
{
while (!culture.Parent.Equals(CultureInfo.InvariantCulture))
culture = culture.Parent;
return culture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment