Replace T4 with Source Generator (#459)

* Replace T4 template with source generator

* remove space

* Added summary

---------

Co-authored-by: Burak Kaan Köse <bkaankose@outlook.com>
This commit is contained in:
Tiktack
2024-11-03 15:51:08 +01:00
committed by GitHub
parent 89ffc5d246
commit 550c8fb899
7 changed files with 235 additions and 3160 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
using Wino.Core.SourceGeneration.Translator;
namespace Wino.Core.Domain;
/// <summary>
/// Translator class for translation.
/// All translations generated automatically by the source generator.
/// </summary>
[TranslatorGen]
public partial class Translator;

View File

@@ -1,60 +0,0 @@
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Text.Json" #>
<#@ assembly name="System.Memory" #>
<#@ assembly name="System" #>
<#@ import namespace="System.Text.Json" #>
<#@ import namespace="System" #>
<#@ assembly name="NetStandard" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension="Designer.cs" #>
<# string filename = this.Host.ResolvePath("Translations/en_US/resources.json");
var allText = File.ReadAllText(filename);
var resourceKeys = JsonSerializer.Deserialize<Dictionary<string, string>>(allText);
#>
namespace Wino.Core.Domain
{
public class Translator
{
private static global::Wino.Core.Domain.Translations.WinoTranslationDictionary _dictionary;
public static global::Wino.Core.Domain.Translations.WinoTranslationDictionary Resources
{
get
{
if (_dictionary == null)
{
_dictionary = new global::Wino.Core.Domain.Translations.WinoTranslationDictionary();
}
return _dictionary;
}
}
<#
string[] escapeChars = new string[] { " ", ";", "@", "$", "&", "(",")","-","#",":","!","'","?","{","}","," };
foreach (var key in resourceKeys)
{
// Generate proper allowed variable name by C#
var allowedPropertyName = escapeChars.Aggregate(key.Key, (c1, c2) => c1.Replace(c2, string.Empty));
// There might be null values for some keys. Those will display as (null string) in the Comment;
// The actual translation for the key will be the key itself at runtime.
var beautifiedValue = key.Value == null ? "(null string)" : key.Value;
// We need to trim the line ending literals for comments.
var beautifiedComment = beautifiedValue.Replace('\r',' ').Replace('\n',' ');
#>
/// <summary>
/// <#= beautifiedComment #>
/// </summary>
public static string <#= allowedPropertyName #> => Resources.GetTranslatedString(@"<#= key.Key #>");
<# } #>
}
}

View File

@@ -70,17 +70,14 @@
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="Translator.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Translator.tt</DependentUpon>
</Compile>
<AdditionalFiles Include="Translations\en_US\resources.json" />
</ItemGroup>
<ItemGroup>
<None Update="Translator.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Translator.Designer.cs</LastGenOutput>
</None>
<!-- Source Generators -->
<ProjectReference Include="..\Wino.SourceGenerators\Wino.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>