Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move unit abbreviation strings into culture-specific resource files #1210

Merged
merged 34 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2b9d29f
Generate restext files for resource management
tmilnthorp Feb 21, 2023
d8ce4b4
Generate all abbreviations
tmilnthorp Feb 21, 2023
b6bb840
Rethrow any exceptions, don't eat em
tmilnthorp Feb 21, 2023
e2f1a41
Gen + embed resources
tmilnthorp Feb 21, 2023
d764ede
Remove cultureless
tmilnthorp Feb 21, 2023
89ca024
We do want an assembly default
tmilnthorp Feb 21, 2023
3c47843
Trim on output
tmilnthorp Feb 21, 2023
5fb964c
Add helper methods to quantities
tmilnthorp Feb 21, 2023
cad93b1
Revert
tmilnthorp Feb 21, 2023
40a27a2
Revert
tmilnthorp Feb 21, 2023
29498c1
Remove quotes
tmilnthorp Feb 21, 2023
7289df2
Rerotue methods
tmilnthorp Feb 21, 2023
a8cbb25
Merge remote-tracking branch 'angularsen/master' into tmilntho/resources
tmilnthorp Feb 27, 2023
fce9382
Don't map in unit
tmilnthorp Feb 28, 2023
954e308
Switch UnitAbbreviationsCache implementation
tmilnthorp Mar 1, 2023
b91a9ab
Handle no abbreviations
tmilnthorp Mar 1, 2023
22889e0
Handle custom unit invalid
tmilnthorp Mar 1, 2023
a786d7a
Merge commit '0ca2f1a94b5f7cd9d357b78738877a1ad15cc106' into tmilntho…
tmilnthorp Mar 2, 2023
a986b7b
Expose Abbreviations and lazily load via resources
tmilnthorp Mar 2, 2023
ca51f4c
Cleanup
tmilnthorp Mar 2, 2023
3f9c4e2
Use ConcurrentDictionary
tmilnthorp Mar 2, 2023
16cdcd4
Merge remote-tracking branch 'upstream/master' into tmilntho/resources
tmilnthorp Mar 13, 2023
b1bd359
Remove empty catch/throw
tmilnthorp Mar 13, 2023
146c45c
Move abbreviationss to QuantityInfo
tmilnthorp Mar 13, 2023
a40fdda
Revert
tmilnthorp Mar 13, 2023
e8fd69b
Fix for non-culture IFormatProvider
tmilnthorp Mar 13, 2023
b8a5ead
Implement custom abbreviation mapping
tmilnthorp Mar 14, 2023
47811b7
Merge remote-tracking branch 'angularsen/master' into tmilntho/resources
tmilnthorp Mar 16, 2023
53e765f
Merge remote-tracking branch 'upstream/master' into tmilntho/resources
tmilnthorp Apr 11, 2023
e8f33f0
Merge remote-tracking branch 'upstream/master' into tmilntho/resources
tmilnthorp May 8, 2023
52369d0
Fix for static Quantity generator
tmilnthorp May 8, 2023
e54adb6
Merge remote-tracking branch 'upstream/master' into tmilntho/resources
tmilnthorp May 19, 2023
4666383
Forward to instance
tmilnthorp May 19, 2023
8e8a90f
Merge branch 'master' into tmilntho/resources
angularsen Jun 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void GenerateStaticConstructor()
if (baseUnits == null)
{
Writer.WL($@"
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", BaseUnits.Undefined),");
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", BaseUnits.Undefined, ""{_quantity.Name}""),");
}
else
{
Expand All @@ -152,7 +152,7 @@ private void GenerateStaticConstructor()
}.Where(str => str != null));

Writer.WL($@"
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", new BaseUnits({baseUnitsCtorArgs})),");
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", new BaseUnits({baseUnitsCtorArgs}), ""{_quantity.Name}""),");
}
}

Expand Down Expand Up @@ -359,25 +359,6 @@ internal static void RegisterDefaultConversions(UnitConverter unitConverter)
Writer.WL($@"
}}

internal static void MapGeneratedLocalizations(UnitAbbreviationsCache unitAbbreviationsCache)
{{");
foreach(Unit unit in _quantity.Units)
{
foreach(Localization localization in unit.Localization)
{
// All units must have a unit abbreviation, so fallback to "" for units with no abbreviations defined in JSON
var abbreviationParams = localization.Abbreviations.Any() ?
string.Join(", ", localization.Abbreviations.Select(abbr => $@"""{abbr}""")) :
$@"""""";

Writer.WL($@"
unitAbbreviationsCache.PerformAbbreviationMapping({_unitEnumName}.{unit.SingularName}, new CultureInfo(""{localization.Culture}""), false, {unit.AllowAbbreviationLookup.ToString().ToLower()}, new string[]{{{abbreviationParams}}});");
}
}

Writer.WL($@"
}}

/// <summary>
/// Get unit abbreviation string.
/// </summary>
Expand Down
22 changes: 9 additions & 13 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace UnitsNet
/// <summary>
/// Dynamically parse or construct quantities when types are only known at runtime.
/// </summary>
public static partial class Quantity
public partial class Quantity
{
/// <summary>
/// All QuantityInfo instances mapped by quantity name that are present in UnitsNet by default.
Expand Down Expand Up @@ -63,7 +63,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu
Writer.WL(@"
_ => throw new ArgumentException($""{quantityInfo.Name} is not a supported quantity."")
};
}
}

/// <summary>
/// Try to dynamically construct a quantity.
Expand All @@ -74,26 +74,22 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
public static bool TryFrom(QuantityValue value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity)
{
switch (unit)
quantity = unit switch
{");
foreach (var quantity in _quantities)
{
var quantityName = quantity.Name;
var unitTypeName = $"{quantityName}Unit";
var unitValue = unitTypeName.ToCamelCase();
Writer.WL($@"
case {unitTypeName} {unitValue}:
quantity = {quantityName}.From(value, {unitValue});
return true;");
{unitTypeName} {unitValue} => {quantityName}.From(value, {unitValue}),");
}

Writer.WL(@"
default:
{
quantity = default(IQuantity);
return false;
}
}
_ => null
};

return quantity is not null;
}

/// <summary>
Expand Down Expand Up @@ -125,7 +121,7 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
Writer.WL(@"
_ => false
};
}
}

internal static IEnumerable<Type> GetQuantityTypes()
{");
Expand Down
51 changes: 51 additions & 0 deletions CodeGen/Generators/UnitsNetGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using CodeGen.Generators.UnitsNetGen;
Expand Down Expand Up @@ -71,6 +72,7 @@ public static void Generate(string rootDir, Quantity[] quantities, QuantityNameT
Log.Information("");
GenerateIQuantityTests(quantities, $"{testProjectDir}/GeneratedCode/IQuantityTests.g.cs");
GenerateStaticQuantity(quantities, $"{outputDir}/Quantity.g.cs");
GenerateResourceFiles(quantities, $"{outputDir}/Resources");

var unitCount = quantities.SelectMany(q => q.Units).Count();
Log.Information("");
Expand Down Expand Up @@ -130,5 +132,54 @@ private static void GenerateStaticQuantity(Quantity[] quantities, string filePat
File.WriteAllText(filePath, content);
Log.Information("✅ Quantity.g.cs");
}

private static void GenerateResourceFiles(Quantity[] quantities, string resourcesDirectory)
{
foreach(var quantity in quantities)
{
var cultures = new HashSet<string>();

foreach(Unit unit in quantity.Units)
{
foreach(Localization localization in unit.Localization)
{
cultures.Add(localization.Culture);
}
}

foreach(var culture in cultures)
{
var fileName = culture.Equals("en-US", System.StringComparison.InvariantCultureIgnoreCase) ?
$"{resourcesDirectory}/{quantity.Name}.restext" :
$"{resourcesDirectory}/{quantity.Name}.{culture}.restext";

using var writer = File.CreateText(fileName);

foreach(Unit unit in quantity.Units)
{
foreach(Localization localization in unit.Localization)
{
if(localization.Culture == culture)
{
if(localization.Abbreviations.Any())
{
writer.Write($"{unit.PluralName}=");

for(int i = 0; i < localization.Abbreviations.Length; i++)
{
writer.Write($"{localization.Abbreviations[i]}");

if(i != localization.Abbreviations.Length - 1)
writer.Write(",");
}

writer.WriteLine();
}
}
}
}
}
}
}
}
}
37 changes: 22 additions & 15 deletions UnitsNet/BaseUnits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,28 @@ public override int GetHashCode()
/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();

string GetDefaultAbbreviation<TUnitType>(TUnitType? unitOrNull) where TUnitType : struct, Enum => unitOrNull is { } unit
? UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit)
: "N/A";

sb.AppendFormat("[Length]: {0}, ", GetDefaultAbbreviation(Length));
sb.AppendFormat("[Mass]: {0}, ", GetDefaultAbbreviation(Mass));
sb.AppendFormat("[Time]: {0}, ", GetDefaultAbbreviation(Time));
sb.AppendFormat("[Current]: {0}, ", GetDefaultAbbreviation(Current));
sb.AppendFormat("[Temperature]: {0}, ", GetDefaultAbbreviation(Temperature));
sb.AppendFormat("[Amount]: {0}, ", GetDefaultAbbreviation(Amount));
sb.AppendFormat("[LuminousIntensity]: {0}", GetDefaultAbbreviation(LuminousIntensity));

return sb.ToString();
if(!Equals(Undefined))
{
var sb = new StringBuilder();

string GetDefaultAbbreviation<TUnitType>(TUnitType? unitOrNull) where TUnitType : struct, Enum => unitOrNull is { } unit
? UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit)
: "N/A";

sb.AppendFormat("[Length]: {0}, ", GetDefaultAbbreviation(Length));
sb.AppendFormat("[Mass]: {0}, ", GetDefaultAbbreviation(Mass));
sb.AppendFormat("[Time]: {0}, ", GetDefaultAbbreviation(Time));
sb.AppendFormat("[Current]: {0}, ", GetDefaultAbbreviation(Current));
sb.AppendFormat("[Temperature]: {0}, ", GetDefaultAbbreviation(Temperature));
sb.AppendFormat("[Amount]: {0}, ", GetDefaultAbbreviation(Amount));
sb.AppendFormat("[LuminousIntensity]: {0}", GetDefaultAbbreviation(LuminousIntensity));

return sb.ToString();
}
else
{
return "Undefined";
}
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ public string ToString(IFormatProvider? cultureInfo)
{
cultureInfo = cultureInfo ?? CultureInfo.CurrentCulture;

var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot, cultureInfo);
var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch, cultureInfo);
var footUnit = Length.GetAbbreviation(LengthUnit.Foot, cultureInfo);
var inchUnit = Length.GetAbbreviation(LengthUnit.Inch, cultureInfo);

// Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches".
// So inches are rounded when converting from base units to feet/inches.
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet/CustomCode/Quantities/Mass.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public string ToString(IFormatProvider? cultureInfo)
{
cultureInfo = cultureInfo ?? CultureInfo.CurrentCulture;

var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone, cultureInfo);
var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound, cultureInfo);
var stoneUnit = Mass.GetAbbreviation(MassUnit.Stone, cultureInfo);
var poundUnit = Mass.GetAbbreviation(MassUnit.Pound, cultureInfo);

// Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds".
// So pounds are rounded here.
Expand Down
Loading