Skip to content

Commit

Permalink
Localization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Feb 5, 2025
1 parent b49a7cd commit 566e36e
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 42 deletions.
1 change: 1 addition & 0 deletions Compilation.Roslyn/Compilation.Roslyn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Compilation\Compilation.csproj" />
<ProjectReference Include="..\Localization\Localization.csproj" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion Compilation.Roslyn/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Ecng.Common;
using Ecng.Compilation;
using Ecng.Localization;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -137,7 +138,7 @@ byte[] getBody()
DiagnosticSeverity.Hidden or DiagnosticSeverity.Info => CompilationErrorTypes.Info,
DiagnosticSeverity.Warning => CompilationErrorTypes.Warning,
DiagnosticSeverity.Error => CompilationErrorTypes.Error,
_ => throw new ArgumentOutOfRangeException(nameof(diagnostic), diagnostic.Severity, "Invalid value."),
_ => throw new ArgumentOutOfRangeException(nameof(diagnostic), diagnostic.Severity, "Invalid value.".Localize()),
}
};

Expand Down
4 changes: 3 additions & 1 deletion ComponentModel/AsyncTimeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
using System;
using System.Threading;

using Ecng.Localization;

public class AsyncTimeout
{
public TimeSpan Value { get; }

public AsyncTimeout(TimeSpan value)
{
if (value <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.".Localize());

Value = value;
}
Expand Down
1 change: 1 addition & 0 deletions ComponentModel/ComponentModel.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<ItemGroup>
<ProjectReference Include="..\Localization\Localization.csproj" />
<ProjectReference Include="..\Serialization\Serialization.csproj" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion ComponentModel/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Ecng.ComponentModel
using Ecng.Collections;
using Ecng.Common;
using Ecng.Serialization;
using Ecng.Localization;

public static class Extensions
{
Expand Down Expand Up @@ -223,7 +224,7 @@ public static IEnumerable<PropertyDescriptor> Typed(this PropertyDescriptorColle
throw new ArgumentNullException(nameof(instance));

if (maxDepth < 0)
throw new ArgumentOutOfRangeException(nameof(maxDepth), maxDepth, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(maxDepth), maxDepth, "Invalid value.".Localize());

var properties = TypeDescriptor.GetProperties(instance, [new BasicSettingAttribute()]).Typed();

Expand Down
3 changes: 2 additions & 1 deletion ComponentModel/OperatorRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Ecng.ComponentModel

using Ecng.Collections;
using Ecng.Common;
using Ecng.Localization;

public static class OperatorRegistry
{
Expand Down Expand Up @@ -114,7 +115,7 @@ public static void RemoveOperator<T>(IOperator<T> @operator)
public static long ThrowIfNegative(this long value, string name)
{
if (value < 0)
throw new ArgumentOutOfRangeException(name, value, "Invalid value.");
throw new ArgumentOutOfRangeException(name, value, "Invalid value.".Localize());

return value;
}
Expand Down
6 changes: 6 additions & 0 deletions Ecng.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logging", "Logging\Logging.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server.Utils", "Server.Utils\Server.Utils.csproj", "{3C29E094-0AA7-4CDA-ABD1-A684E8676A10}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Localization", "Localization\Localization.csproj", "{67E1B2C9-483E-4E8D-9617-C4AF7DC1EDC3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -231,6 +233,10 @@ Global
{3C29E094-0AA7-4CDA-ABD1-A684E8676A10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C29E094-0AA7-4CDA-ABD1-A684E8676A10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C29E094-0AA7-4CDA-ABD1-A684E8676A10}.Release|Any CPU.Build.0 = Release|Any CPU
{67E1B2C9-483E-4E8D-9617-C4AF7DC1EDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67E1B2C9-483E-4E8D-9617-C4AF7DC1EDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67E1B2C9-483E-4E8D-9617-C4AF7DC1EDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67E1B2C9-483E-4E8D-9617-C4AF7DC1EDC3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions Localization/ILocalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Ecng.Localization;

public interface ILocalizer
{
public string Localize(string enStr);
}
6 changes: 6 additions & 0 deletions Localization/Localization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
</Project>
39 changes: 39 additions & 0 deletions Localization/LocalizedStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Ecng.Localization;

using Ecng.Common;

public static class LocalizedStrings
{
public static ILocalizer Localizer { get; set; }

public const string InheritedKey = nameof(InheritedKey);
public const string VerboseKey = nameof(VerboseKey);
public const string DebugKey = nameof(DebugKey);
public const string InfoKey = nameof(InfoKey);
public const string WarningsKey = nameof(WarningsKey);
public const string ErrorsKey = nameof(ErrorsKey);
public const string OffKey = nameof(OffKey);
public const string IdKey = nameof(IdKey);
public const string LoggingKey = nameof(LoggingKey);
public const string NameKey = nameof(NameKey);
public const string LogSourceNameKey = nameof(LogSourceNameKey);
public const string LogLevelKey = nameof(LogLevelKey);
public const string LogLevelDescKey = nameof(LogLevelDescKey);

public static string Inherited => Localize(nameof(Inherited));
public static string Verbose => Localize(nameof(Verbose));
public static string Debug => Localize(nameof(Debug));
public static string Info => Localize(nameof(Info));
public static string Warnings => Localize(nameof(Warnings));
public static string Errors => Localize(nameof(Errors));
public static string Off => Localize(nameof(Off));
public static string Id => Localize(nameof(Id));
public static string Logging => Localize(nameof(Logging));
public static string Name => Localize(nameof(Name));
public static string LogSourceName => Localize(nameof(LogSourceName));
public static string LogLevel => Localize(nameof(LogLevel));
public static string LogLevelDesc => Localize(nameof(LogLevelDesc));

public static string Localize(this string enStr)
=> (Localizer?.Localize(enStr)).IsEmpty(enStr);
}
4 changes: 3 additions & 1 deletion Logging/ConsoleLogListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Ecng.Logging;

using Ecng.Localization;

/// <summary>
/// The logger that records the data to the console window.
/// </summary>
Expand All @@ -26,7 +28,7 @@ protected override void OnWriteMessage(LogMessage message)
LogLevels.Verbose or LogLevels.Debug or LogLevels.Info => ConsoleHelper.Info,
LogLevels.Warning => ConsoleHelper.Warning,
LogLevels.Error => ConsoleHelper.Error,
_ => throw new ArgumentOutOfRangeException(nameof(message), message.Level, "Invalid value."),
_ => throw new ArgumentOutOfRangeException(nameof(message), message.Level, "Invalid value.".Localize()),
};
var newLine = "{0} | {1, -15} | {2}".Put(message.Time.ToString(TimeFormat), message.Source.Name, message.Message);

Expand Down
4 changes: 3 additions & 1 deletion Logging/DebugLogListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Ecng.Logging;

using System.Text;

using Ecng.Localization;

/// <summary>
/// The strategy logger that records the data to the debug window.
/// </summary>
Expand Down Expand Up @@ -56,7 +58,7 @@ private static void Dump(LogLevels level, StringBuilder builder)
Trace.TraceError(str);
break;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value.".Localize());
}

builder.Clear();
Expand Down
12 changes: 7 additions & 5 deletions Logging/FileLogListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Ecng.Logging;
using System.IO.Compression;
using System.Text;

using Ecng.Localization;

/// <summary>
/// Modes of log files splitting by date.
/// </summary>
Expand Down Expand Up @@ -136,7 +138,7 @@ public long MaxLength
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.".Localize());

_maxLength = value;
}
Expand All @@ -153,7 +155,7 @@ public int MaxCount
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid value.".Localize());

_maxCount = value;
}
Expand Down Expand Up @@ -296,7 +298,7 @@ private string GetFileName(string sourceName, DateTime date)
Directory.CreateDirectory(dirName);
break;
default:
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(SeparateByDates), SeparateByDates, "Invalid value.".Localize());
}

fileName = Path.Combine(dirName, fileName);
Expand Down Expand Up @@ -330,7 +332,7 @@ private void TryDoHistoryPolicy()
isDir = true;
break;
default:
throw new ArgumentOutOfRangeException(nameof(SeparateByDates), SeparateByDates, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(SeparateByDates), SeparateByDates, "Invalid value.".Localize());
}

if (SeparateByDates == SeparateByDateModes.None)
Expand All @@ -356,7 +358,7 @@ private void TryDoHistoryPolicy()
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(HistoryPolicy), policy, "Invalid value.");
throw new ArgumentOutOfRangeException(nameof(HistoryPolicy), policy, "Invalid value.".Localize());
}

var files = new List<string>();
Expand Down
38 changes: 20 additions & 18 deletions Logging/ILogSource.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Ecng.Logging;

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

using Ecng.ComponentModel;
using Ecng.Localization;

/// <summary>
/// Logs source interface.
Expand Down Expand Up @@ -65,25 +67,25 @@ protected BaseLogSource()

/// <inheritdoc />
[Browsable(false)]
//[Display(
// ResourceType = typeof(LocalizedStrings),
// Name = LocalizedStrings.IdKey,
// Description = LocalizedStrings.IdKey,
// GroupName = LocalizedStrings.LoggingKey,
// Order = 1000)]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.IdKey,
Description = LocalizedStrings.IdKey,
GroupName = LocalizedStrings.LoggingKey,
Order = 1000)]
[ReadOnly(true)]
public virtual Guid Id { get; set; } = Guid.NewGuid();

private string _name;

/// <inheritdoc />
[ReadOnly(true)]
//[Display(
// ResourceType = typeof(LocalizedStrings),
// Name = LocalizedStrings.NameKey,
// Description = LocalizedStrings.LogSourceNameKey,
// GroupName = LocalizedStrings.LoggingKey,
// Order = 1001)]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.NameKey,
Description = LocalizedStrings.LogSourceNameKey,
GroupName = LocalizedStrings.LoggingKey,
Order = 1001)]
public virtual string Name
{
get => _name;
Expand Down Expand Up @@ -125,12 +127,12 @@ public ILogSource Parent
public event Action<ILogSource> ParentRemoved;

/// <inheritdoc />
//[Display(
// ResourceType = typeof(LocalizedStrings),
// Name = LocalizedStrings.LogLevelKey,
// Description = LocalizedStrings.LogLevelKey + LocalizedStrings.Dot,
// GroupName = LocalizedStrings.LoggingKey,
// Order = 1001)]
[Display(
ResourceType = typeof(LocalizedStrings),
Name = LocalizedStrings.LogLevelKey,
Description = LocalizedStrings.LogLevelDescKey,
GroupName = LocalizedStrings.LoggingKey,
Order = 1001)]
public virtual LogLevels LogLevel { get; set; } = LogLevels.Inherit;

/// <inheritdoc />
Expand Down
17 changes: 10 additions & 7 deletions Logging/LogLevels.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Ecng.Logging;

using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

using Ecng.Localization;

/// <summary>
/// Levels of log messages <see cref="LogMessage"/>.
/// </summary>
Expand All @@ -11,49 +14,49 @@ public enum LogLevels
/// <summary>
/// To use the logging level of the container.
/// </summary>
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.InheritedKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.InheritedKey)]
[EnumMember]
Inherit,

/// <summary>
/// Verbose message, debug message, information, warnings and errors.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.VerboseKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.VerboseKey)]
Verbose,

/// <summary>
/// Debug message, information, warnings and errors.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.DebugKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.DebugKey)]
Debug,

/// <summary>
/// Information, warnings and errors.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.InfoKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.InfoKey)]
Info,

/// <summary>
/// Warnings and errors.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.WarningsKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.WarningsKey)]
Warning,

/// <summary>
/// Errors only.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.ErrorsKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.ErrorsKey)]
Error,

/// <summary>
/// Logs off.
/// </summary>
[EnumMember]
//[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.OffKey)]
[Display(ResourceType = typeof(LocalizedStrings), Name = LocalizedStrings.OffKey)]
Off,
}
1 change: 1 addition & 0 deletions Logging/Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<Import Project="..\common.props" />
<ItemGroup>
<ProjectReference Include="..\ComponentModel\ComponentModel.csproj" />
<ProjectReference Include="..\Localization\Localization.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 566e36e

Please sign in to comment.