Skip to content

Commit

Permalink
[FIX] WebAssembly does not have full CultureInfo, leading to language…
Browse files Browse the repository at this point in the history
… mapping failures
  • Loading branch information
StepKie committed Jan 14, 2024
1 parent aa0993a commit fb14b3e
Show file tree
Hide file tree
Showing 6 changed files with 4,140 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</PropertyGroup>
<PropertyGroup>
<BlazorWebAssemblyEnableAOT>false</BlazorWebAssemblyEnableAOT>
<!-- Unfortunately, while this does load globalization data, the languages do not have their full English names set, only the short language codes -->
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.34.0" />
Expand Down
5 changes: 2 additions & 3 deletions MtgCsvHelper.BlazorWebAssembly/Pages/MtgCsvProcessor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@
isConverting = true;
StateHasChanged();
await Task.Delay(1);
var stream = csvFile!.OpenReadStream(maxAllowedSize: MAX_FILE_SIZE);

Stream csvStream = new MemoryStream();
Stream csvStream = new MemoryStream();
// We need an async stream to read the file contents
await csvFile!.OpenReadStream(maxAllowedSize: MAX_FILE_SIZE).CopyToAsync(csvStream);
Expand All @@ -136,7 +135,7 @@

var cards = collection.Cards;
processedRecords = cards.Select(c => c.Printing.Name).ToList();
resultFileName ??= $"{selectedOutputFormat}-output-{DateTime.Now:yyyy-MM-dd}.csv";
resultFileName ??= $"{selectedOutputFormat}-output-{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.csv";
writer.WriteCollectionCsv(cards, resultFileName);

}
Expand Down
2 changes: 1 addition & 1 deletion MtgCsvHelper.Tests/ScryfallApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public async Task DownloadTokensTest()
{
var tokenCards = await _api.GetTokenCardNamesAsync();
var tokenNames = tokenCards.Select(c => c.Name).Distinct().ToList();
tokenNames.Count().Should().BeGreaterThan(100);
tokenNames.Count.Should().BeGreaterThan(100);
}
}
34 changes: 17 additions & 17 deletions MtgCsvHelper/Converters/LanguageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ namespace MtgCsvHelper.Converters;

public class LanguageConverter(LanguageConfiguration configuration) : ITypeConverter
{
// We have used CultureInfo.GetCultures(CultureTypes.NeutralCultures) before
// but in WebAssembly, the names are shortened and dont contain the full name in CultureInfo.EnglishName
// hence, we use our own dictionary
static readonly Dictionary<string, string> _languages = new()
{
["English"] = "en",
["Spanish"] = "es",
["French"] = "fr",
["German"] = "de",
["Italian"] = "it",
["Portuguese"] = "pt",
["Japanese"] = "jp",
};

readonly bool _useShortNames = configuration.ShortNames;

public object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
Expand All @@ -15,28 +29,14 @@ public class LanguageConverter(LanguageConfiguration configuration) : ITypeConve
{
null or "" => null,
_ when _useShortNames => text,
_ => GetLanguageCode(text)

_ => _languages.GetValueOrDefault(text),
};

string? GetLanguageCode(string englishName)
{
var culture = CultureInfo.GetCultures(CultureTypes.NeutralCultures).FirstOrDefault(c => c.EnglishName.Equals(englishName));
if (culture is null)
{
Log.Warning($"Unable to find language code for {englishName}");
return null;
}

return culture.TwoLetterISOLanguageName;
}
}

public string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
{
if (value is null || value is not string langCode) { return null; }
var cultureInfo = new CultureInfo(langCode);
return _useShortNames ? langCode : cultureInfo.EnglishName;
if (value is not string langCode) { return null; }
return _useShortNames ? langCode : _languages.FirstOrDefault(x => x.Value == langCode).Key;
}
}

2 changes: 1 addition & 1 deletion MtgCsvHelper/MtgCardCsvHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void CheckIfFirstLineCanBeIgnored(StreamReader stream)

public void WriteCollectionCsv(IList<PhysicalMtgCard> cards, string? outputFileName = null)
{
outputFileName ??= $"{_format.ToLower()}-output-{DateTime.Now:yyyy-MM-dd}.csv";
outputFileName ??= $"{_format.ToLower()}-output-{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.csv";
Log.Information($"Writing {cards.Sum(c => c.Count)} cards ({cards.Count} unique) cards to {outputFileName}");

using var writer = new StreamWriter(outputFileName);
Expand Down
Loading

0 comments on commit fb14b3e

Please sign in to comment.