Skip to content

Commit

Permalink
qodana
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jan 8, 2024
1 parent 0558ec8 commit cd90467
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
25 changes: 12 additions & 13 deletions src/sly/buildresult/BuildResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace sly.buildresult
{
public class BuildResult<R>
{
public BuildResult() : this(default(R))
public BuildResult() : this(default)
{ }

public BuildResult(R result)
Expand All @@ -22,7 +22,7 @@ public BuildResult(R result)

public bool IsError
{
get { return Errors.Any<InitializationError>(e => e.Level != ErrorLevel.WARN); }
get { return Errors.Any(e => e.Level != ErrorLevel.WARN); }
}

public bool IsOk => !IsError;
Expand All @@ -37,30 +37,29 @@ public void AddInitializationError(ErrorLevel level, string message, ErrorCodes
Errors.Add(new InitializationError(level,message,errorCode));
}

public void AddErrors(List<InitializationError> errors)
public void AddErrors(IEnumerable<InitializationError> errors)
{
Errors.AddRange(errors);
}


[ExcludeFromCodeCoverage]
public override string ToString()
{
if (IsOk)
{
return $"parser is ok {typeof(R)}";
}
else
{
StringBuilder error = new StringBuilder();
error.AppendLine("parser is KO");
foreach (var initializationError in Errors)
{
error.AppendLine($"{initializationError.Level} - {initializationError.Code} : {initializationError.Message}");
}

return error.ToString();
var error = new StringBuilder();
error.AppendLine("parser is KO");
foreach (var initializationError in Errors)
{
error.AppendLine(
$"{initializationError.Level} - {initializationError.Code} : {initializationError.Message}");
}

return error.ToString();
}
}
}
6 changes: 3 additions & 3 deletions src/sly/buildresult/InitializationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public InitializationError(ErrorLevel level, string message, ErrorCodes code)
Code = code;
}

public ErrorLevel Level { get; set; }
public ErrorLevel Level { get; }

public string Message { get; set; }
public string Message { get; }

public ErrorCodes Code {get; set;}
public ErrorCodes Code {get; }
}
}
34 changes: 8 additions & 26 deletions src/sly/i18n/I18N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,42 @@ public class I18N

private static I18N _instance;

public static I18N Instance
{
get
{
if (_instance == null)
{
_instance = new I18N();
}

return _instance;
}
}
public static I18N Instance => _instance ?? (_instance = new I18N());

protected I18N()
private I18N()
{
Translations = new Dictionary<string, IDictionary<I18NMessage, string>>();
}

public string GetText(string lang, I18NMessage key, params string[] args)
{
lang = lang ?? CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
IDictionary<I18NMessage, string> translation = new Dictionary<I18NMessage, string>();
if (!Translations.TryGetValue(lang, out translation))
if (!Translations.TryGetValue(lang, out var translation))
{
translation = Load(lang);
}

string pattern = null;
if (translation.TryGetValue(key, out pattern))
{
return string.Format(pattern, args);
}

return "";

return translation.TryGetValue(key, out var pattern) ? string.Format(pattern, args) : "";

Check warning on line 30 in src/sly/i18n/I18N.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Co-variant array conversion

Co-variant array conversion from string\[\] to object?\[\] can cause run-time exception on write operation

Check warning on line 30 in src/sly/i18n/I18N.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Co-variant array conversion

Co-variant array conversion from string\[\] to object\[\] can cause run-time exception on write operation
}


private IDictionary<I18NMessage,string> Load(string lang)
{
var translation = new Dictionary<I18NMessage, string>();
Assembly assembly = GetType().Assembly;
var res = assembly.GetManifestResourceNames();
using (var stream = assembly.GetManifestResourceStream($"sly.i18n.translations.{lang}.txt"))
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
using (var reader = new StreamReader(stream))
{
string line = reader.ReadLine();
var line = reader.ReadLine();
while (line != null)
{
if (!line.StartsWith("#"))
{
var items = line.Split(new[] {'='});
var items = line.Split('=');
if (items.Length == 2)
{
var key = EnumConverter.ConvertStringToEnum<I18NMessage>(items[0]);
Expand Down

0 comments on commit cd90467

Please sign in to comment.