-
Notifications
You must be signed in to change notification settings - Fork 0
/
Language.cs
82 lines (76 loc) · 2.47 KB
/
Language.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Collections.Frozen;
using System.Globalization;
namespace Cyberia.Langzilla.Enums;
/// <summary>
/// Language of the lang.
/// </summary>
public enum Language
{
/// <summary>
/// French.
/// </summary>
fr,
/// <summary>
/// English.
/// </summary>
en,
/// <summary>
/// Spanish.
/// </summary>
es,
/// <summary>
/// Deutsch.
/// </summary>
de,
/// <summary>
/// Italian.
/// </summary>
it,
/// <summary>
/// Netherlands.
/// </summary>
nl,
/// <summary>
/// Portuguese.
/// </summary>
pt
}
/// <summary>
/// Provides extension methods for the <see cref="Language"/> enum.
/// </summary>
public static class LanguageExtensions
{
private static readonly FrozenDictionary<Language, CultureInfo> s_cachedCultureByLanguage = new Dictionary<Language, CultureInfo>()
{
{ Language.fr, CultureInfo.GetCultureInfo("fr") },
{ Language.en, CultureInfo.GetCultureInfo("en") },
{ Language.es, CultureInfo.GetCultureInfo("es") },
{ Language.de, CultureInfo.GetCultureInfo("de") },
{ Language.it, CultureInfo.GetCultureInfo("it") },
{ Language.nl, CultureInfo.GetCultureInfo("nl") },
{ Language.pt, CultureInfo.GetCultureInfo("pt") }
}.ToFrozenDictionary();
public static string ToStringFast(this Language language)
{
return Enum.GetName(language) ?? language.ToString();
}
/// <summary>
/// Converts a <see cref="Language"/> to its corresponding <see cref="CultureInfo"/>.
/// </summary>
/// <param name="language">The language to convert.</param>
/// <returns>The corresponding <see cref="CultureInfo"/>; if not found, the <see cref="CultureInfo"/> for <see cref="Language.en"/>.</returns>
public static CultureInfo ToCulture(this Language language)
{
s_cachedCultureByLanguage.TryGetValue(language, out var result);
return result ?? s_cachedCultureByLanguage[Language.en];
}
/// <summary>
/// Converts a <see cref="CultureInfo"/> to its corresponding <see cref="Language"/>.
/// </summary>
/// <param name="culture">The culture to convert.</param>
/// <returns>The corresponding <see cref="Language"/>; if not found, <see cref="Language.en"/>.</returns>
public static Language ToLangLanguage(this CultureInfo culture)
{
return Enum.TryParse<Language>(culture.TwoLetterISOLanguageName, out var result) ? result : Language.en;
}
}