From 2bcacaf0955f21b8069279d095f7af2ce0cf99c4 Mon Sep 17 00:00:00 2001 From: capdiem Date: Thu, 6 Jun 2024 00:37:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20:=20use=20the=20CompositeFormat=20A?= =?UTF-8?q?PI=20of=20.NET=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Masa.Blazor/I18n/I18n.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Masa.Blazor/I18n/I18n.cs b/src/Masa.Blazor/I18n/I18n.cs index 4ea096676e..12ee5ac72e 100644 --- a/src/Masa.Blazor/I18n/I18n.cs +++ b/src/Masa.Blazor/I18n/I18n.cs @@ -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; @@ -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) { @@ -122,4 +128,16 @@ private void SetCultureAndLocale(CultureInfo uiCulture) Culture = uiCulture; Locale = I18nCache.GetLocale(uiCulture); } -} \ No newline at end of file +} +#if NET8_0_OR_GREATER + +public static class CompositeFormatCache +{ + private static readonly ConcurrentDictionary Cache = new(); + + public static CompositeFormat Parse(CultureInfo cultureInfo, string format) + { + return Cache.GetOrAdd($"{cultureInfo.Name}-{format}", CompositeFormat.Parse); + } +} +#endif \ No newline at end of file