Files
radzen-blazor/RadzenBlazorDemos/Pages/DocumentProcessingLocalization.razor

64 lines
2.7 KiB
Plaintext
Raw Normal View History

Add culture-aware editing, display, and formula entry to RadzenSpreadsheet The spreadsheet engine parsed typed input with the invariant culture while displaying values with the current culture, breaking the edit round-trip on non-en-US hosts and making comma-decimal entry (10,50) impossible. Workbook gains a Culture property (defaults to CurrentCulture) which the component stamps from its inherited Culture parameter. It drives: - Cell input parsing (CellData type inference), including day-month date handling for cultures where the group and date separators collide (de-DE) - Edit text and display rendering (GetValue, GetValueAsString, GetDisplayText) - Number format rendering - format codes stay canonical invariant tokens while separators, month names, and AM/PM designators follow the culture - Formula entry and display via the new FormulaLocalizer (Excel FormulaLocal semantics: ';' argument separators and ',' decimals in comma-decimal cultures, lenient comma acceptance where unambiguous, canonical invariant storage) - Dialog input (data validation, conditional format, filter) via shared conversion helpers on SpreadsheetDialogBase XLSX and CSV files read and write canonical invariant values regardless of the workbook culture, including autofit column widths. Malformed formulas now surface as error trees instead of an unhandled lexer exception. Number format parsing is cached (hard-bounded) since CellView reparsed per render. Includes localization demos for the Spreadsheet and Document Processing sections and culture test suites. Note: headless code on non-en-US hosts now parses string values with the host culture; set Workbook.Culture explicitly (or to InvariantCulture) for host-independent processing.
2026-07-08 14:54:40 +03:00
@using System.Globalization
@using Radzen.Documents.Spreadsheet
<RadzenText TextStyle="TextStyle.Body1" TagName="TagName.P" class="rz-pb-2">
Each row below is a <code>Workbook</code> with a different <code>Culture</code>. The canonical
<code>Cell.Formula</code> stays identical in every culture - only user-facing entry and display change.
<code>Workbook.Culture</code> defaults to <code>CultureInfo.CurrentCulture</code>; set it explicitly in
server-side code so results do not depend on the host locale.
</RadzenText>
<RadzenDataGrid Data="@rows" TItem="CultureRow" AllowPaging="false" class="rz-pb-4">
<Columns>
<RadzenDataGridColumn Property="@nameof(CultureRow.Culture)" Title="Culture" Width="90px" />
<RadzenDataGridColumn Property="@nameof(CultureRow.LocalFormula)" Title="Formula as the user types it" />
<RadzenDataGridColumn Property="@nameof(CultureRow.CanonicalFormula)" Title="Canonical Cell.Formula" />
<RadzenDataGridColumn Property="@nameof(CultureRow.FormattedNumber)" Title="1234.5 as #,##0.00" />
<RadzenDataGridColumn Property="@nameof(CultureRow.ParsedInput)" Title="Typing &quot;10,50&quot; produces" />
</Columns>
</RadzenDataGrid>
@code {
class CultureRow
{
public string Culture { get; set; } = string.Empty;
public string LocalFormula { get; set; } = string.Empty;
public string CanonicalFormula { get; set; } = string.Empty;
public string FormattedNumber { get; set; } = string.Empty;
public string ParsedInput { get; set; } = string.Empty;
}
readonly List<CultureRow> rows = [];
protected override void OnInitialized()
{
foreach (var name in new[] { "en-US", "de-DE", "es-ES", "fr-FR", "it-IT", "ja-JP" })
{
var workbook = new Workbook { Culture = CultureInfo.GetCultureInfo(name) };
var sheet = workbook.AddSheet("Sheet1", 5, 5);
sheet.Cells["A1"].Value = 1000;
sheet.Cells["A2"].Value = 234.5;
var formulaCell = sheet.Cells["B1"];
formulaCell.Formula = "=SUM(A1,A2)*1.5";
var formattedCell = sheet.Cells["C1"];
formattedCell.Value = 1234.5;
formattedCell.Format.NumberFormat = "#,##0.00";
var inputCell = sheet.Cells["D1"];
inputCell.SetValue("10,50");
rows.Add(new CultureRow
{
Culture = name,
LocalFormula = formulaCell.GetValue(),
CanonicalFormula = formulaCell.Formula,
FormattedNumber = formattedCell.GetDisplayText(),
ParsedInput = $"{Convert.ToString(inputCell.Value, CultureInfo.InvariantCulture)} ({inputCell.ValueType})"
});
}
}
}