Skip to content

Commit 63971e2

Browse files
committed
Revert "refactor: 合并代码提高可读性"
This reverts commit 47809a0.
1 parent e025e33 commit 63971e2

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,23 @@ public override LocalizedString this[string name]
5151
{
5252
get
5353
{
54-
string? value = null;
55-
try
56-
{
57-
var format = GetStringSafely(name);
58-
value = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments);
59-
}
60-
catch (Exception ex)
54+
var value = SafeFormat();
55+
return new LocalizedString(name, value ?? name, resourceNotFound: value == null, searchedLocation: typeName);
56+
57+
string? SafeFormat()
6158
{
62-
Logger.LogError(ex, "{JsonStringLocalizerName} searched for '{Name}' in '{typeName}' with culture '{CultureName}' throw exception.", nameof(JsonStringLocalizer), name, typeName, CultureInfo.CurrentUICulture.Name);
59+
string? ret = null;
60+
try
61+
{
62+
var format = GetStringSafely(name);
63+
ret = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments);
64+
}
65+
catch (Exception ex)
66+
{
67+
Logger.LogError(ex, "{JsonStringLocalizerName} searched for '{Name}' in '{typeName}' with culture '{CultureName}' throw exception.", nameof(JsonStringLocalizer), name, typeName, CultureInfo.CurrentUICulture.Name);
68+
}
69+
return ret;
6370
}
64-
return new LocalizedString(name, value ?? name, resourceNotFound: value == null, searchedLocation: typeName);
6571
}
6672
}
6773

@@ -98,13 +104,7 @@ public override LocalizedString this[string name]
98104
var cacheKey = $"{nameof(GetValueFromCache)}&name={name}&{Assembly.GetUniqueName()}&type={typeName}&culture={cultureName}";
99105
if (!CacheManager.GetMissingLocalizerByKey(cacheKey))
100106
{
101-
LocalizedString? localizer = null;
102-
if (localizerStrings != null)
103-
{
104-
// perf: 性能问题,这里需要优化
105-
localizer = localizerStrings.FirstOrDefault(i => i.Name == name);
106-
}
107-
var l = localizer ?? CacheManager.GetAllStringsFromResolve().FirstOrDefault(i => i.Name == name);
107+
var l = GetLocalizedString();
108108
if (l is { ResourceNotFound: false })
109109
{
110110
ret = l.Value;
@@ -116,6 +116,16 @@ public override LocalizedString this[string name]
116116
}
117117
}
118118
return ret;
119+
120+
LocalizedString? GetLocalizedString()
121+
{
122+
LocalizedString? localizer = null;
123+
if (localizerStrings != null)
124+
{
125+
localizer = localizerStrings.FirstOrDefault(i => i.Name == name);
126+
}
127+
return localizer ?? CacheManager.GetAllStringsFromResolve().FirstOrDefault(i => i.Name == name);
128+
}
119129
}
120130

121131
private string? GetLocalizerValueFromCache(IStringLocalizer localizer, string name)

0 commit comments

Comments
 (0)