-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unit abbreviation strings into culture-specific resource files (#…
…1210) Fixes #1126 Refs #1238 A quick draft to move the unit abbreviations into resource files. I also want to move methods for getting culture-specific abbreviations to the individual quantities. This should: - Reduce initialization time by removing the MapGeneratedLocalizations methods (TODO) - Reduce in-memory footprint by only loading a requested culture - Allow formatting to use methods on the quantities themselves - Which should allow for better extensibility as well - Remove the unit abbreviations cache (deprecate) ### TODOs - [ ] Test fallback to invariant culture `export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='1'` #1238 --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
- Loading branch information
1 parent
4a02911
commit 37ada11
Showing
282 changed files
with
3,901 additions
and
4,652 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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(""); | ||
|
@@ -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(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.