-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Support for Nested Classes #8
- Loading branch information
Showing
25 changed files
with
739 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
src/kli.Localize.Generator/Internal/GeneratorDataContext.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace kli.Localize.Generator.Internal.Helper | ||
{ | ||
internal class CultureData | ||
{ | ||
public const string InvariantKeyName = "invariant"; | ||
|
||
public string Key { get; private set; } | ||
public string NormalizedKey { get; private set; } | ||
public TranslationData Translations { get; private set; } | ||
|
||
private CultureData() { } | ||
|
||
public static IReadOnlyList<CultureData> Initialize(string filePath, ITranslationReader translationReader) | ||
{ | ||
var searchDir = Path.GetDirectoryName(filePath)!; | ||
var fileName = Path.GetFileNameWithoutExtension(filePath); | ||
return Directory.GetFiles(searchDir, $"{fileName}*{Path.GetExtension(filePath)}") | ||
.Select(cfp => ResolveCulture(cfp, translationReader)).ToList(); | ||
} | ||
|
||
private static CultureData ResolveCulture(string cultureFilePath, ITranslationReader translationReader) | ||
{ | ||
var cultureId = Path.GetFileNameWithoutExtension(cultureFilePath).Split('_').Skip(1).LastOrDefault() | ||
?? InvariantKeyName; | ||
|
||
return new() | ||
{ | ||
Key = cultureId, | ||
NormalizedKey = NormalizeCultureIdentifier(cultureId), | ||
Translations = translationReader.Read(cultureFilePath) | ||
}; | ||
} | ||
|
||
private static string NormalizeCultureIdentifier(string cultureId) | ||
=> cultureId.ToLower().Replace('-', '_'); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/kli.Localize.Generator/Internal/Helper/GeneratorData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace kli.Localize.Generator.Internal.Helper | ||
{ | ||
internal class GeneratorData | ||
{ | ||
public IReadOnlyList<CultureData> CultureData { get; set; } | ||
public TranslationData InvariantTranslationData | ||
=> this.CultureData.Single(cd => cd.Key == Helper.CultureData.InvariantKeyName).Translations; | ||
|
||
public string Namespace { get; set; } | ||
public string GeneratedFileName { get; set; } | ||
public string GeneratedClassName { get; set; } | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/kli.Localize.Generator/Internal/Helper/GeneratorDataBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace kli.Localize.Generator.Internal.Helper; | ||
|
||
internal class GeneratorDataBuilder(AdditionalText originFile, | ||
NamesResolver namesResolver, ITranslationReader translationReader) | ||
{ | ||
public GeneratorData Build() | ||
{ | ||
return new GeneratorData | ||
{ | ||
GeneratedClassName = namesResolver.ResolveGeneratedClassName(), | ||
GeneratedFileName = namesResolver.ResolveGeneratedFileName(), | ||
Namespace = namesResolver.ResolveNamespace(), | ||
CultureData = CultureData.Initialize(originFile.Path, translationReader), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/kli.Localize.Generator/Internal/Helper/TranslationData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace kli.Localize.Generator.Internal.Helper | ||
{ | ||
internal class TranslationData : Dictionary<string, object> | ||
{ | ||
public IDictionary<string, string> Flatten() | ||
=> this.FlattenCore(this, string.Empty); | ||
|
||
private IDictionary<string, string> FlattenCore(TranslationData translationData, string parentKey) | ||
{ | ||
var result = new Dictionary<string, string>(); | ||
foreach (var kvp in translationData) | ||
{ | ||
var key = string.IsNullOrEmpty(parentKey) ? kvp.Key : $"{parentKey}::{kvp.Key}"; | ||
if (kvp.Value is TranslationData nested) | ||
{ | ||
foreach (var nestedKvp in this.FlattenCore(nested, key)) | ||
result[nestedKvp.Key] = nestedKvp.Value; | ||
} | ||
else | ||
result[key] = kvp.Value.ToString(); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using kli.Localize.Generator.Internal.Helper; | ||
|
||
namespace kli.Localize.Generator.Internal | ||
{ | ||
internal interface ITranslationReader | ||
{ | ||
TranslationData Read(string filePath); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/kli.Localize.Generator/Internal/Json/JsonDiagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace kli.Localize.Generator.Internal.Json | ||
{ | ||
internal static class JsonDiagnostics | ||
{ | ||
public static void ReportInvalidFileFormat(this Action<Diagnostic> reporter, string filePath, JsonReaderException ex) | ||
{ | ||
reporter.Report(1, ex.Message, filePath, | ||
new LinePosition(ex.LineNumber-1, ex.LinePosition), DiagnosticSeverity.Error); | ||
} | ||
|
||
public static void ReportInvalidKey(this Action<Diagnostic> reporter, string filePath, JsonTextReader reader) | ||
{ | ||
var message = $"Json property key must be a valid C# identifier: '{reader.Value}'"; | ||
reporter.Report(2, message, filePath, | ||
new LinePosition(reader.LineNumber-1, reader.LinePosition), DiagnosticSeverity.Warning); | ||
} | ||
|
||
public static void ReportInvalidTokenType(this Action<Diagnostic> reporter, string filePath, JsonTextReader reader) | ||
{ | ||
var message = $"Json property value must be an object or a string: '{reader.TokenType} - {reader.Value} ({reader.ValueType})'"; | ||
reporter.Report(3, message, filePath, | ||
new LinePosition(reader.LineNumber-1, reader.LinePosition), DiagnosticSeverity.Warning); | ||
} | ||
|
||
private static void Report(this Action<Diagnostic> reporter, int id, string message, | ||
string filePath, LinePosition linePosition, DiagnosticSeverity severity) | ||
{ | ||
var diagnostic = Diagnostic.Create( | ||
new DiagnosticDescriptor($"SGL000{id}", "kli.Localize.Generator", message, "Source Generators", severity, true), | ||
Location.Create(filePath, new TextSpan(), new LinePositionSpan(linePosition, linePosition))); | ||
reporter(diagnostic); | ||
} | ||
} | ||
} |
Oops, something went wrong.