Skip to content

Commit

Permalink
Updated to LocalizeDefault version preview032
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPSmith committed Dec 15, 2022
1 parent 038032e commit f3ff0e1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview030" />
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview032" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion AuthPermissions.BaseCode/AuthPermissions.BaseCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview030" />
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview032" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions AuthPermissions.BaseCode/DataLayer/Classes/AuthUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ private static IStatusGeneric CheckRolesAreValidForUser(List<RoleToPermissions>
switch (tenantUser)
{
case true when foundRole.RoleType == RoleTypes.HiddenFromTenant:
status.AddErrorFormatted("InvalidRoleHidden".LocalizeKeyBuilder(typeof(AuthUser), true, false, true),
status.AddErrorFormatted("InvalidRoleHidden".StaticClassLocalizeKey(typeof(AuthUser), true),
$"You cannot add the role '{foundRole.RoleName}' to an Auth tenant user because it can only be used by the App Admin.");
break;
case true when foundRole.RoleType == RoleTypes.TenantAutoAdd:
status.AddErrorFormatted("InvalidRoleAutoAdd".LocalizeKeyBuilder(typeof(AuthUser), true, false, true),
status.AddErrorFormatted("InvalidRoleAutoAdd".StaticClassLocalizeKey(typeof(AuthUser), true),
$"You cannot add the role '{foundRole.RoleName}' to an Auth tenant user because it is automatically to tenant users.");
break;
}
Expand Down
2 changes: 1 addition & 1 deletion AuthPermissions.BaseCode/DataLayer/Classes/Tenant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private static IStatusGeneric<Tenant> CheckRolesAreAllTenantRolesAndSetTenantRol

foreach (var badRole in badRoles)
{
status.AddErrorFormatted("RoleNotTenantRole".LocalizeKeyBuilder(typeof(Tenant),true, false, true),
status.AddErrorFormatted("RoleNotTenantRole".StaticClassLocalizeKey(typeof(Tenant), true),
$"The Role '{badRole.RoleName}' is not a tenant role, i.e. only roles with a {nameof(RoleToPermissions.RoleType)} of ",
$"{nameof(RoleTypes.TenantAutoAdd)} or {nameof(RoleTypes.TenantAdminAdd)} can be added to a tenant.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ private static IStatusGeneric ConvertExceptionToStatus(this IReadOnlyList<Entity
return exceptionType switch
{
ExceptionTypes.Duplicate => status.AddErrorFormatted(
"DuplicateDb".LocalizeKeyBuilder(typeof(SaveChangesExtensions), true, false, true),
"DuplicateDb".StaticClassLocalizeKey(typeof(SaveChangesExtensions), true),
$"There is already a {typeName} with a value: name = {name}"),
ExceptionTypes.ConcurrencyError => status.AddErrorFormatted(
"ConcurrencyError".LocalizeKeyBuilder(typeof(SaveChangesExtensions), true, false, true),
"ConcurrencyError".StaticClassLocalizeKey(typeof(SaveChangesExtensions), true),
$"Another user changed the {typeName} with the name = {name}. Please re-read the entity and add you change again."),
_ => throw new ArgumentOutOfRangeException(nameof(exceptionType), exceptionType, null)
};
}

//This shouldn't happen, but just in case
status.AddErrorFormatted(
"UnknownException".LocalizeKeyBuilder(typeof(SaveChangesExtensions), true, false, true),
"UnknownException".StaticClassLocalizeKey(typeof(SaveChangesExtensions), true),
$"There was a {exceptionType} on an auth class.");

return status;
Expand Down
2 changes: 1 addition & 1 deletion AuthPermissions/AuthPermissions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview030" />
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview032" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 33 additions & 17 deletions Test/StubClasses/StubLocalizeWithDefaultWithLogging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using LocalizeMessagesAndErrors;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand All @@ -25,14 +28,15 @@ public class StubLocalizeDefaultWithLogging<TResource> : ILocalizeWithDefault<TR
/// </summary>
public List<LocalizedLog> Logs { get; set; } = new List<LocalizedLog>();


/// <summary>
/// This is true if there is an entry in the database with the same Resource/Key, but a different.
/// Used in tests of this class.
/// This can contain a possible error. Null if no possible error found.
/// </summary>
public bool? SameKeyButDiffFormat { get; set; }

public string? PossibleError { get; set; }

/// <summary>
/// Start of a possible error
/// </summary>
public const string SameKeyButDiffFormatPrefix = "Possible SameKeyButDiffFormat: ";

public string LocalizeStringMessage(LocalizeKeyData localizeKeyData, string cultureOfMessage, string message)
{
Expand Down Expand Up @@ -89,34 +93,46 @@ private LocalizedLog CreateLocalizedLog(LocalizeKeyData localizeKeyData, string
/// <param name="localizedLog"></param>
private void SaveLocalizationToDb(LocalizedLog localizedLog)
{

using var context = GetLocalizationCaptureDbInstance();
if (context == null)
return;

//This will hold any existing database entries that have the same ResourceFile and LocalizeKey
var sameLocalizationReference = context.LocalizedData
//This will hold any existing database entries that have the same ResourceFile and LocalizeKey or format
var sameKeyOrFormat = context.LocalizedData
.Where(x => x.ResourceClassFullName == localizedLog.ResourceClassFullName
&& x.LocalizeKey == localizedLog.LocalizeKey).ToList();
&& (x.LocalizeKey == localizedLog.LocalizeKey
|| (x.MessageFormat != null && x.MessageFormat != localizedLog.MessageFormat)
|| (x.MessageFormat == null && x.ActualMessage != localizedLog.ActualMessage))).ToList();

//This is true if there was already an entry with the same ResourceFile / LocalizeKey but a different message
SameKeyButDiffFormat = sameLocalizationReference.Any(x =>
x.ResourceClassFullName == typeof(TResource).FullName
&& x.LocalizeKey == localizedLog.LocalizeKey
&& ((x.MessageFormat != null && x.MessageFormat != localizedLog.MessageFormat)
|| (x.MessageFormat == null && x.ActualMessage != localizedLog.ActualMessage)));
PossibleError = null;

//This is true if there was already an entry with the same LocalizeKey but a different message
var keyButDiffFormatIds = sameKeyOrFormat.
Where(x => x.LocalizeKey == localizedLog.LocalizeKey
&& ((x.MessageFormat != null && x.MessageFormat != localizedLog.MessageFormat)
|| (x.MessageFormat == null && x.ActualMessage != localizedLog.ActualMessage))
&& x.PossibleErrors == null)
.Select(x => x.Id).ToArray();

if (keyButDiffFormatIds.Any())
{
PossibleError = SameKeyButDiffFormatPrefix +
$"entry(s) with same key have IDs of {string.Join(", ", keyButDiffFormatIds.Select(x => x.ToString()))}";
}

if (sameLocalizationReference.Any(x =>
if (sameKeyOrFormat.Any(x =>
((x.MessageFormat != null && x.MessageFormat == localizedLog.MessageFormat) //Using format: and the same
|| (x.MessageFormat == null && x.ActualMessage == localizedLog.ActualMessage)) //Using string: and the same
&& x.SameKeyButDiffFormat == SameKeyButDiffFormat
&& PossibleError == x.PossibleErrors
&& x.CallingClassName == localizedLog.CallingClassName
&& x.CallingMethodName == localizedLog.CallingMethodName
&& x.SourceLineNumber == localizedLog.SourceLineNumber))
//There is already an entry with the same
return;

//set the SameKey before writing to the database
localizedLog.SameKeyButDiffFormat = SameKeyButDiffFormat;
localizedLog.PossibleErrors = PossibleError;

context.Add(localizedLog);
context.SaveChanges();
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview030" />
<PackageReference Include="Net.LocalizeMessagesAndErrors" Version="1.0.0-preview032" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 5 additions & 3 deletions Test/UnitCommands/LocalizationCaptureCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public void DisplayCapturedLocalizations()

var entries = stub.ListLocalizationCaptureDb();

_output.WriteLine($"There are {entries.Count} captured localizations, with {entries.Count(x => x.SameKeyButDiffFormat != false)} so problems.");
_output.WriteLine($"There are {entries.Count} captured localizations, " +
$"with {entries.Count(x => x.PossibleErrors != null)} so problems.");
foreach (var entry in entries)
{
_output.WriteLine($"ResourceClassFullName = {entry.ResourceClassFullName}, LocalizeKey = {entry.LocalizeKey}, {(entry.SameKeyButDiffFormat != false ? "BAD" : "")}");
_output.WriteLine($"ResourceClassFullName = {entry.ResourceClassFullName}");
_output.WriteLine($" LocalizeKey = {entry.LocalizeKey}, {entry.PossibleErrors ?? ""}");
_output.WriteLine($" Actual Message = {entry.ActualMessage}");
if (entry.MessageFormat != null )
if (entry.MessageFormat != null)
_output.WriteLine($" Message Format = {entry.MessageFormat}");
}
_output.WriteLine("END ------------------------------------------------------");
Expand Down

0 comments on commit f3ff0e1

Please sign in to comment.