|
| 1 | +using System; |
1 | 2 | using System.Collections.Generic;
|
2 | 3 | using System.Linq;
|
3 | 4 | using CodeGen.JsonTypes;
|
4 | 5 |
|
5 | 6 | namespace CodeGen
|
6 | 7 | {
|
7 | 8 | /// <summary>
|
8 |
| - /// Information about a unit prefix and a static dictionary to look up prefixes. |
| 9 | + /// Information about a unit prefix and a static dictionary to look up prefixes. |
9 | 10 | /// </summary>
|
10 | 11 | internal class PrefixInfo
|
11 | 12 | {
|
12 |
| - /// <summary> |
13 |
| - /// The unit prefix. |
14 |
| - /// </summary> |
15 |
| - public Prefix Prefix { get; } |
16 |
| - |
17 |
| - /// <summary> |
18 |
| - /// The unit prefix abbreviation, such as "k" for kilo or "m" for milli. |
19 |
| - /// </summary> |
20 |
| - public string Abbreviation { get; } |
21 |
| - |
22 |
| - /// <summary> |
23 |
| - /// C# expression for the multiplier to prefix the conversion function. |
24 |
| - /// </summary> |
25 |
| - /// <example>Kilo has "1e3" in order to multiply by 1000.</example> |
26 |
| - public string Factor { get; } |
| 13 | + private const string Russian = "ru-RU"; |
27 | 14 |
|
28 | 15 | public static readonly IReadOnlyDictionary<Prefix, PrefixInfo> Entries = new[]
|
29 | 16 | {
|
30 | 17 | // Need to append 'd' suffix for double in order to later search/replace "d" with "m"
|
31 | 18 | // when creating decimal conversion functions in CodeGen.Generator.FixConversionFunctionsForDecimalValueTypes.
|
32 | 19 |
|
33 | 20 | // SI prefixes
|
34 |
| - new PrefixInfo(Prefix.Yocto, "y", "1e-24d"), |
35 |
| - new PrefixInfo(Prefix.Zepto, "z", "1e-21d"), |
36 |
| - new PrefixInfo(Prefix.Atto, "a", "1e-18d"), |
37 |
| - new PrefixInfo(Prefix.Femto, "f", "1e-15d"), |
38 |
| - new PrefixInfo(Prefix.Pico, "p", "1e-12d"), |
39 |
| - new PrefixInfo(Prefix.Nano, "n", "1e-9d"), |
40 |
| - new PrefixInfo(Prefix.Micro, "µ", "1e-6d"), |
41 |
| - new PrefixInfo(Prefix.Milli, "m", "1e-3d"), |
42 |
| - new PrefixInfo(Prefix.Centi, "c", "1e-2d"), |
43 |
| - new PrefixInfo(Prefix.Deci, "d", "1e-1d"), |
44 |
| - new PrefixInfo(Prefix.Deca, "da", "1e1d"), |
45 |
| - new PrefixInfo(Prefix.Hecto, "h", "1e2d"), |
46 |
| - new PrefixInfo(Prefix.Kilo, "k", "1e3d"), |
47 |
| - new PrefixInfo(Prefix.Mega, "M", "1e6d"), |
48 |
| - new PrefixInfo(Prefix.Giga, "G", "1e9d"), |
49 |
| - new PrefixInfo(Prefix.Tera, "T", "1e12d"), |
50 |
| - new PrefixInfo(Prefix.Peta, "P", "1e15d"), |
51 |
| - new PrefixInfo(Prefix.Exa, "E", "1e18d"), |
52 |
| - new PrefixInfo(Prefix.Zetta, "Z", "1e21d"), |
53 |
| - new PrefixInfo(Prefix.Yotta, "Y", "1e24d"), |
| 21 | + new PrefixInfo(Prefix.Yocto, "1e-24d", "y"), |
| 22 | + new PrefixInfo(Prefix.Zepto, "1e-21d", "z"), |
| 23 | + new PrefixInfo(Prefix.Atto, "1e-18d", "a", (Russian, "а")), |
| 24 | + new PrefixInfo(Prefix.Femto, "1e-15d", "f", (Russian, "ф")), |
| 25 | + new PrefixInfo(Prefix.Pico, "1e-12d", "p", (Russian, "п")), |
| 26 | + new PrefixInfo(Prefix.Nano, "1e-9d", "n", (Russian, "н")), |
| 27 | + new PrefixInfo(Prefix.Micro, "1e-6d", "µ", (Russian, "мк")), |
| 28 | + new PrefixInfo(Prefix.Milli, "1e-3d", "m", (Russian, "м")), |
| 29 | + new PrefixInfo(Prefix.Centi, "1e-2d", "c", (Russian, "с")), |
| 30 | + new PrefixInfo(Prefix.Deci, "1e-1d", "d", (Russian, "д")), |
| 31 | + new PrefixInfo(Prefix.Deca, "1e1d", "da", (Russian, "да")), |
| 32 | + new PrefixInfo(Prefix.Hecto, "1e2d", "h", (Russian, "г")), |
| 33 | + new PrefixInfo(Prefix.Kilo, "1e3d", "k", (Russian, "к")), |
| 34 | + new PrefixInfo(Prefix.Mega, "1e6d", "M", (Russian, "М")), |
| 35 | + new PrefixInfo(Prefix.Giga, "1e9d", "G", (Russian, "Г")), |
| 36 | + new PrefixInfo(Prefix.Tera, "1e12d", "T", (Russian, "Т")), |
| 37 | + new PrefixInfo(Prefix.Peta, "1e15d", "P", (Russian, "П")), |
| 38 | + new PrefixInfo(Prefix.Exa, "1e18d", "E", (Russian, "Э")), |
| 39 | + new PrefixInfo(Prefix.Zetta, "1e21d", "Z"), |
| 40 | + new PrefixInfo(Prefix.Yotta, "1e24d", "Y"), |
54 | 41 |
|
55 | 42 | // Binary prefixes
|
56 |
| - new PrefixInfo(Prefix.Kibi, "Ki", $"1024d"), |
57 |
| - new PrefixInfo(Prefix.Mebi, "Mi", $"(1024d * 1024)"), |
58 |
| - new PrefixInfo(Prefix.Gibi, "Gi", $"(1024d * 1024 * 1024)"), |
59 |
| - new PrefixInfo(Prefix.Tebi, "Ti", $"(1024d * 1024 * 1024 * 1024)"), |
60 |
| - new PrefixInfo(Prefix.Pebi, "Pi", $"(1024d * 1024 * 1024 * 1024 * 1024)"), |
61 |
| - new PrefixInfo(Prefix.Exbi, "Ei", $"(1024d * 1024 * 1024 * 1024 * 1024 * 1024)"), |
| 43 | + new PrefixInfo(Prefix.Kibi, "1024d", "Ki"), |
| 44 | + new PrefixInfo(Prefix.Mebi, "(1024d * 1024)", "Mi"), |
| 45 | + new PrefixInfo(Prefix.Gibi, "(1024d * 1024 * 1024)", "Gi"), |
| 46 | + new PrefixInfo(Prefix.Tebi, "(1024d * 1024 * 1024 * 1024)", "Ti"), |
| 47 | + new PrefixInfo(Prefix.Pebi, "(1024d * 1024 * 1024 * 1024 * 1024)", "Pi"), |
| 48 | + new PrefixInfo(Prefix.Exbi, "(1024d * 1024 * 1024 * 1024 * 1024 * 1024)", "Ei") |
62 | 49 | }.ToDictionary(prefixInfo => prefixInfo.Prefix);
|
63 | 50 |
|
64 |
| - private PrefixInfo(Prefix prefix, string abbreviation, string factor) |
| 51 | + private PrefixInfo(Prefix prefix, string factor, string siPrefix, params (string cultureName, string prefix)[] cultureToPrefix) |
65 | 52 | {
|
66 | 53 | Prefix = prefix;
|
67 |
| - Abbreviation = abbreviation; |
| 54 | + SiPrefix = siPrefix; |
| 55 | + CultureToPrefix = cultureToPrefix; |
68 | 56 | Factor = factor;
|
69 | 57 | }
|
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// The unit prefix. |
| 61 | + /// </summary> |
| 62 | + public Prefix Prefix { get; } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// C# expression for the multiplier to prefix the conversion function. |
| 66 | + /// </summary> |
| 67 | + /// <example>Kilo has "1e3" in order to multiply by 1000.</example> |
| 68 | + public string Factor { get; } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// The unit prefix abbreviation, such as "k" for kilo or "m" for milli. |
| 72 | + /// </summary> |
| 73 | + private string SiPrefix { get; } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Mapping from culture name to localized prefix abbreviation. |
| 77 | + /// </summary> |
| 78 | + private (string cultureName, string prefix)[] CultureToPrefix { get; } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets the localized prefix if configured, otherwise <see cref="SiPrefix" />. |
| 82 | + /// </summary> |
| 83 | + /// <param name="cultureName">Culture name, such as "en-US" or "ru-RU".</param> |
| 84 | + public string GetPrefixForCultureOrSiPrefix(string cultureName) |
| 85 | + { |
| 86 | + if (cultureName == null) throw new ArgumentNullException(nameof(cultureName)); |
| 87 | + |
| 88 | + var localizedPrefix = CultureToPrefix |
| 89 | + .Where(x => string.Equals(x.cultureName, cultureName, StringComparison.OrdinalIgnoreCase)) |
| 90 | + .Select(x => x.prefix).FirstOrDefault(); |
| 91 | + |
| 92 | + return localizedPrefix ?? SiPrefix; |
| 93 | + } |
70 | 94 | }
|
71 | 95 | }
|
0 commit comments