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

added support for column name localization #56

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,23 @@
<Folder Include="SampleFiles\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="TestResources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>TestResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="TestResources.cs-CZ.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TestResources.cs-CZ.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="TestResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TestResources.resx</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Bogus;
using FluentAssertions;
Expand Down Expand Up @@ -110,6 +111,33 @@ public void CreateMultiSheetSpreadsheet_ShouldThrowError_WhenProvidedSheetsIsNul
Assert.Equal("exportSheets", actualResult.ParamName);
}

[Fact]
public void CreateSheet_With_Localized_ColumnName()
{
var configuration = new SpreadsheetConfiguration<LocalizedSampleExportRecord>
{
WorksheetName = "TestSheet",
ExportData = new List<LocalizedSampleExportRecord>()
{
new()
{
RecordTitle = "test record"
}
}
};

CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("cs-CZ");

using var ms = new MemoryStream();
var result = _spreadsheetGenerator.CreateSingleSheetSpreadsheet(ms, configuration);

// ms.Seek(0, SeekOrigin.Begin);
// File.WriteAllBytes(@"d:\dcore\test.xlsx", ms.ToArray());

result.Should().BeTrue();
ms.Should().NotHaveLength(0);
}
Comment on lines +114 to +139
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method CreateSheet_With_Localized_ColumnName is well-implemented for generating a spreadsheet with a localized column name. Consider adding a verification step to ensure that the column names are correctly localized in the resulting file.

Would you like assistance in implementing this verification step?


private static Faker<TestExportRecord> GetTestExportRecordFaker() =>
new Faker<TestExportRecord>()
.UseSeed(1415)
Expand Down
6 changes: 6 additions & 0 deletions src/NetCore.Utilities.Spreadsheet.Tests/SampleExportRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public class DifferentTestExportRecord

}

public class LocalizedSampleExportRecord
{
[SpreadsheetColumn(ResourceFileType = typeof(TestResources), ResourceKey = nameof(TestResources.LocalizedColumn))]
public string? RecordTitle { get; set; }
}

public class SampleExportRecordWithFormula
{
[DisplayName("Title")]
Expand Down
71 changes: 71 additions & 0 deletions src/NetCore.Utilities.Spreadsheet.Tests/TestResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/NetCore.Utilities.Spreadsheet.Tests/TestResources.cs-CZ.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="LocalizedColumn" xml:space="preserve">
<value>Lokalizovaný sloupec</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/NetCore.Utilities.Spreadsheet.Tests/TestResources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="LocalizedColumn" xml:space="preserve">
<value>LocalizedColumn</value>
</data>
</root>
12 changes: 11 additions & 1 deletion src/NetCore.Utilities.Spreadsheet/SpreadsheetColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// <param name="ignore">If true, the column will be excluded from the spreadsheet</param>
/// <param name="format">Sets the format of the column data</param>
public SpreadsheetColumnAttribute(string displayName = null, float width = 0, bool ignore = false,
string format = null, string formula = null)

Check warning on line 22 in src/NetCore.Utilities.Spreadsheet/SpreadsheetColumnAttribute.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Parameter 'formula' has no matching param tag in the XML comment for 'SpreadsheetColumnAttribute.SpreadsheetColumnAttribute(string, float, bool, string, string)' (but other parameters do)

Check warning on line 22 in src/NetCore.Utilities.Spreadsheet/SpreadsheetColumnAttribute.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Parameter 'formula' has no matching param tag in the XML comment for 'SpreadsheetColumnAttribute.SpreadsheetColumnAttribute(string, float, bool, string, string)' (but other parameters do)
{
DisplayName = displayName;
Width = width;
Expand Down Expand Up @@ -50,7 +50,17 @@
/// A custom format for the column. See <see cref="ColumnFormats" /> for valid values.
/// </summary>
public string Format { get; }


/// <summary>
/// If set, will use the resource file and key to get the display name
/// </summary>
public Type ResourceFileType { get; set; }

/// <summary>
/// If set, will use the resource file and key to get the display name
/// </summary>
public string ResourceKey { get; set; }

/// <summary>
/// A custom formula for the column. For example SUM, MIN, MAX, etc.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/NetCore.Utilities.Spreadsheet/TypeDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Resources;
using System.Text.RegularExpressions;

namespace ICG.NetCore.Utilities.Spreadsheet;
Expand Down Expand Up @@ -50,6 +51,11 @@ public static IList<PropDetail> GetProps(Type t)
format = (sca.Format ?? format).ToLowerInvariant();
propName = sca.DisplayName ?? propName;
width = sca.Width;
if (sca.ResourceFileType != null && string.IsNullOrWhiteSpace(sca.ResourceKey) == false)
{
ResourceManager rm = new(sca.ResourceFileType);
propName = rm.GetString(sca.ResourceKey) ?? propName;
}
Comment on lines +54 to +58
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation for retrieving localized property names using ResourceManager is robust. Consider adding error handling for potential exceptions from ResourceManager.GetString.

+ try
+ {
+     propName = rm.GetString(sca.ResourceKey) ?? propName;
+ }
+ catch (Exception ex)
+ {
+     // Handle exception (e.g., log it)
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (sca.ResourceFileType != null && string.IsNullOrWhiteSpace(sca.ResourceKey) == false)
{
ResourceManager rm = new(sca.ResourceFileType);
propName = rm.GetString(sca.ResourceKey) ?? propName;
}
if (sca.ResourceFileType != null && string.IsNullOrWhiteSpace(sca.ResourceKey) == false)
{
ResourceManager rm = new(sca.ResourceFileType);
try
{
propName = rm.GetString(sca.ResourceKey) ?? propName;
}
catch (Exception ex)
{
// Handle exception (e.g., log it)
}
}

formula = sca.Formula;
}
else if (attr is DisplayAttribute display)
Expand Down
Loading