-
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.
- Loading branch information
Showing
27 changed files
with
750 additions
and
362 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 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); | ||
} | ||
} |
Oops, something went wrong.