Skip to content

Commit

Permalink
fix: add argument count mismatch warnings for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
lodicolo committed Jan 28, 2025
1 parent 5d49f06 commit cf7f8c2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 29 deletions.
59 changes: 38 additions & 21 deletions Framework/Intersect.Framework.Core/Localization/LocalizedString.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
namespace Intersect.Localization;
using System.Text.RegularExpressions;

namespace Intersect.Localization;

[Serializable]
public partial class LocalizedString : Localized
public partial class LocalizedString(string value) : Localized
{
private readonly string _value = value;

private readonly string mValue;
public int ArgumentCount { get; } = CountArguments(value);

public LocalizedString(string value)
{
mValue = value;
}
public static implicit operator LocalizedString(string value) => new(value);

public static implicit operator LocalizedString(string value)
{
return new LocalizedString(value);
}
public static implicit operator string(LocalizedString localizedString) => localizedString._value;

public static implicit operator string(LocalizedString str)
{
return str.mValue;
}

public override string ToString()
{
return mValue;
}
public override string ToString() => _value;

public string ToString(params object[] args)
{
try
{
return args?.Length == 0 ? mValue : string.Format(mValue, args ?? new object[] { });
return args?.Length == 0 ? _value : string.Format(_value, args ?? []);
}
catch (FormatException)
{
return "Format Exception!";
}
}

private static readonly Regex PatternArgument = new(
"\\{(?<argumentIndex>\\d+)\\}",
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.NonBacktracking
);

public static int CountArguments(string formatString)
{
HashSet<int> argumentIndices = [];

var matches = PatternArgument.Matches(formatString);
foreach (Match match in matches)
{
if (!match.Success)
{
continue;
}

var rawArgumentIndex = match.Groups["argumentIndex"].Value;
if (!int.TryParse(rawArgumentIndex, out var argumentIndex))
{
continue;
}

_ = argumentIndices.Add(argumentIndex);
}

return argumentIndices.Count;
}
}
48 changes: 40 additions & 8 deletions Intersect.Client.Core/Localization/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static void Load()

var rootType = typeof(Strings);
var groupTypes = rootType.GetNestedTypes(BindingFlags.Static | BindingFlags.Public);
var missingStrings = new List<string>();
List<string> missingStrings = [];
List<string> argumentCountMismatch = [];
foreach (var groupType in groupTypes)
{
if (!serialized.TryGetValue(groupType.Name, out var serializedGroup))
Expand Down Expand Up @@ -148,7 +149,25 @@ public static void Load()
}
else
{
fieldInfo.SetValue(null, new LocalizedString(jsonString));
LocalizedString? existingLocalizedString = null;
try
{
existingLocalizedString = fieldInfo.GetValue(null) as LocalizedString;
}
catch
{
// Ignore
}

LocalizedString newLocalizedString = new(jsonString);
if (existingLocalizedString?.ArgumentCount != newLocalizedString.ArgumentCount)
{
argumentCountMismatch.Add(
$"{groupType.Name}.{fieldInfo.Name} expected {existingLocalizedString?.ArgumentCount.ToString() ?? "ERROR"} argument(s) but the loaded string had {newLocalizedString.ArgumentCount}"
);
}

fieldInfo.SetValue(null, newLocalizedString);
}
break;

Expand Down Expand Up @@ -191,16 +210,17 @@ public static void Load()
break;
}

_ = _methodInfoDeserializeDictionary.MakeGenericMethod(parameters.First()).Invoke(default, new object[]
{
missingStrings,
_ = _methodInfoDeserializeDictionary.MakeGenericMethod(parameters.First()).Invoke(default,
[
missingStrings,
groupType,
fieldInfo,
fieldValue,
serializedGroup,
serializedValue,
fieldValue
});
fieldValue,
]
);
break;
}
}
Expand All @@ -209,9 +229,21 @@ public static void Load()

if (missingStrings.Count > 0)
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Missing strings, overwriting strings file:\n\t{string.Join(",\n\t", missingStrings)}");
ApplicationContext.Context.Value?.Logger.LogWarning(
"Missing strings, overwriting strings file:\n\t{Strings}",
string.Join(",\n\t", missingStrings)
);
SaveSerialized(serialized);
}

if (argumentCountMismatch.Count > 0)
{
ApplicationContext.Context.Value?.Logger.LogWarning(
"Argument count mismatch on {MismatchCount} strings:\n\t{Strings}",
argumentCountMismatch.Count,
string.Join(",\n\t", argumentCountMismatch)
);
}
}
catch (Exception exception)
{
Expand Down

0 comments on commit cf7f8c2

Please sign in to comment.