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

⚡perf(I18n) : use the CompositeFormat API of .NET 8 #1953

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 22 additions & 4 deletions src/Masa.Blazor/I18n/I18n.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Options;
#if NET8_0_OR_GREATER
using System.Collections.Concurrent;
#endif
using System.Diagnostics.CodeAnalysis;

namespace Masa.Blazor;

Expand Down Expand Up @@ -104,7 +106,11 @@ public string T(string? key, params object[] args)

try
{
return string.Format(value, args);
#if NET8_0_OR_GREATER
return string.Format(Culture, CompositeFormatCache.Parse(Culture, value), args);
#else
return string.Format(Culture, value, args);
#endif
}
catch (FormatException)
{
Expand All @@ -122,4 +128,16 @@ private void SetCultureAndLocale(CultureInfo uiCulture)
Culture = uiCulture;
Locale = I18nCache.GetLocale(uiCulture);
}
}
}
#if NET8_0_OR_GREATER

public static class CompositeFormatCache
{
private static readonly ConcurrentDictionary<string, CompositeFormat> Cache = new();

public static CompositeFormat Parse(CultureInfo cultureInfo, string format)
{
return Cache.GetOrAdd($"{cultureInfo.Name}-{format}", CompositeFormat.Parse);
}
}
#endif