Skip to content

Commit

Permalink
Merge pull request #89 from Ashampoo/develop
Browse files Browse the repository at this point in the history
release
  • Loading branch information
tjorvenK authored Sep 9, 2024
2 parents ef55a6a + 64046d8 commit 3555dc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,6 @@ public record FormatStringOption(string Name, bool Required = false) : FormatOpt
public string Value { get; set; } = "";
}

/// <summary>
/// Format option for a char value.
/// </summary>
/// <param name="Name">
/// <inheritdoc cref="FormatOption"/>
/// </param>
/// <param name="Required">
/// <inheritdoc cref="FormatOption"/>
/// </param>
public record FormatCharacterOption(string Name, bool Required = false) : FormatOption(Name, Required)
{
/// <summary>
/// The value of the option.
/// </summary>
public string Value { get; set; } = ";";
}

/// <summary>
/// Configuration object for a <see cref="IFormat"/> containing a list of <see cref="FormatOption">FormatOptions</see>.
/// </summary>
Expand Down
50 changes: 31 additions & 19 deletions src/Ashampoo.Translation.Systems.Formats/src/CSV/CsvFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,44 @@ private async Task GetHeaderInformation(StreamReader reader, FormatReadOptions o
switch (headerLine[0].Trim())
{
case "#Delimiter":
if (char.IsWhiteSpace(Delimiter) && !string.IsNullOrWhiteSpace(headerLine[1]))
{
CsvFormatHeader.Delimiter = headerLine[1].Trim().ToCharArray().First();
}

TryApplyDelimiter(headerLine);
break;
case "#Source Language":
if (string.IsNullOrWhiteSpace(options.SourceLanguage?.Value) &&
!string.IsNullOrWhiteSpace(headerLine[1]))
{
Header.SourceLanguage = new Language(headerLine[1].Trim());
}

TryApplySourceLanguage(options, headerLine);
break;
case "#Target Language":
if (string.IsNullOrWhiteSpace(options.TargetLanguage.Value) &&
!string.IsNullOrWhiteSpace(headerLine[1]))
{
Header.TargetLanguage = new Language(headerLine[1].Trim());
}

TryApplyTargetLanguage(options, headerLine);
break;
}
}
}

private void TryApplyTargetLanguage(FormatReadOptions options, string[] headerLine)
{
if (string.IsNullOrWhiteSpace(options.TargetLanguage.Value) &&
!string.IsNullOrWhiteSpace(headerLine[1]))
{
Header.TargetLanguage = new Language(headerLine[1].Trim());
}
}

private void TryApplySourceLanguage(FormatReadOptions options, string[] headerLine)
{
if (string.IsNullOrWhiteSpace(options.SourceLanguage?.Value) &&
!string.IsNullOrWhiteSpace(headerLine[1]))
{
Header.SourceLanguage = new Language(headerLine[1].Trim());
}
}

private void TryApplyDelimiter(string[] headerLine)
{
if (char.IsWhiteSpace(Delimiter) && !string.IsNullOrWhiteSpace(headerLine[1]))
{
CsvFormatHeader.Delimiter = headerLine[1].Trim()[0];
}
}

private async Task ReadCsv(CsvReader reader)
{
await reader.ReadAsync();
Expand Down Expand Up @@ -200,7 +212,7 @@ private async Task<bool> ConfigureOptionsAsync(FormatReadOptions options)

FormatStringOption targetLanguageOption = new("Target language", true);
FormatStringOption sourceLanguageOption = new("Source language", true);
FormatCharacterOption delimiterOption = new("Delimiter", true);
FormatStringOption delimiterOption = new("Delimiter", true);

List<FormatOption> optionList = [];
if (setTargetLanguage) optionList.Add(targetLanguageOption);
Expand Down Expand Up @@ -237,7 +249,7 @@ private async Task<bool> ConfigureOptionsAsync(FormatReadOptions options)
}
}

file record CsvRecordFormat
file sealed record CsvRecordFormat
{
[Name("id")] public string Id { get; init; } = string.Empty;

Expand Down

0 comments on commit 3555dc8

Please sign in to comment.