init project
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
157
src/GameIdeas/GameIdeas.Resources/CreateStaticResourceKey.tt
Normal file
157
src/GameIdeas/GameIdeas.Resources/CreateStaticResourceKey.tt
Normal file
@@ -0,0 +1,157 @@
|
||||
<#@ template debug="true" hostspecific="true" language="C#" #>
|
||||
<#@ assembly name="System.Xml.dll" #>
|
||||
<#@ import namespace="System.Xml" #>
|
||||
<#@ import namespace="System.IO" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".cs" #>
|
||||
|
||||
<#
|
||||
var path = Path.GetDirectoryName(Host.TemplateFile);
|
||||
var localPath = Host.ResolvePath(Path.Combine(path,"../../Server/GameIdeas.API/Files/GameIdeas.fr.json"));
|
||||
|
||||
var json = File.ReadAllText(localPath);
|
||||
|
||||
var index = 0;
|
||||
|
||||
|
||||
Dictionary<string, string?> ParseToDictionary()
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (index >= json.Length || json[index] != '{')
|
||||
throw new Exception("Invalid JSON: Root must be an object.");
|
||||
|
||||
return ParseObjectToDictionary();
|
||||
}
|
||||
|
||||
Dictionary<string, string?> ParseObjectToDictionary()
|
||||
{
|
||||
var dict = new Dictionary<string, string?>();
|
||||
Consume('{');
|
||||
|
||||
while (true)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (json[index] == '}') break;
|
||||
|
||||
var key = ParseString();
|
||||
SkipWhitespace();
|
||||
Consume(':');
|
||||
SkipWhitespace();
|
||||
var value = ParseValueAsString();
|
||||
dict[key] = value;
|
||||
|
||||
SkipWhitespace();
|
||||
if (json[index] == ',') Consume(',');
|
||||
else if (json[index] == '}') break;
|
||||
else throw new Exception("Expected ',' or '}' in object.");
|
||||
}
|
||||
|
||||
Consume('}');
|
||||
return dict;
|
||||
}
|
||||
|
||||
string ParseString()
|
||||
{
|
||||
Consume('"');
|
||||
var start = index;
|
||||
while (json[index] != '"')
|
||||
{
|
||||
if (json[index] == '\\') index++; // Skip escape character
|
||||
index++;
|
||||
}
|
||||
|
||||
var result = json.Substring(start, index - start);
|
||||
Consume('"');
|
||||
return result.Replace("\\\"", "\""); // Handle escaped quotes
|
||||
}
|
||||
|
||||
string? ParseValueAsString()
|
||||
{
|
||||
SkipWhitespace();
|
||||
|
||||
if (json[index] == '"') return ParseString();
|
||||
if (char.IsDigit(json[index]) || json[index] == '-' || json[index] == '.') return ParseNumber();
|
||||
if (json[index] == 't') { ConsumeLiteral("true"); return "true"; }
|
||||
if (json[index] == 'f') { ConsumeLiteral("false"); return "false"; }
|
||||
if (json[index] == 'n') { ConsumeLiteral("null"); return null; }
|
||||
if (json[index] == '{' || json[index] == '[')
|
||||
throw new Exception("Nested objects or arrays are not supported in this implementation.");
|
||||
|
||||
throw new Exception($"Unexpected character at index {index}");
|
||||
}
|
||||
|
||||
string ParseNumber()
|
||||
{
|
||||
var start = index;
|
||||
if (json[index] == '-') index++; // Handle negative sign
|
||||
while (index < json.Length && char.IsDigit(json[index])) index++;
|
||||
if (index < json.Length && json[index] == '.') // Handle fractional part
|
||||
{
|
||||
index++;
|
||||
while (index < json.Length && char.IsDigit(json[index])) index++;
|
||||
}
|
||||
return json.Substring(start, index - start);
|
||||
}
|
||||
|
||||
void ConsumeLiteral(string literal)
|
||||
{
|
||||
for (int i = 0; i < literal.Length; i++)
|
||||
{
|
||||
if (index >= json.Length || json[index] != literal[i])
|
||||
throw new Exception($"Expected '{literal}' but found '{json[index]}'");
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void Consume(char expected)
|
||||
{
|
||||
if (json[index] != expected)
|
||||
throw new Exception($"Expected '{expected}' but found '{json[index]}'");
|
||||
index++;
|
||||
}
|
||||
|
||||
void SkipWhitespace()
|
||||
{
|
||||
while (index < json.Length && char.IsWhiteSpace(json[index]))
|
||||
index++;
|
||||
}
|
||||
// Deserialize the JSON content to a dictionary
|
||||
//var translations = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
|
||||
var translations = ParseToDictionary();
|
||||
|
||||
// Generate the class
|
||||
#>
|
||||
using System;
|
||||
namespace ArgosV2.Resources;
|
||||
public class Translations (TranslationService translationService)
|
||||
{
|
||||
<#
|
||||
// Iterate through the dictionary and generate properties
|
||||
foreach (var translation in translations)
|
||||
{
|
||||
#>
|
||||
public string <#= translation.Key #> => translationService.Translate(nameof(<#= translation.Key #>));
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
|
||||
public static class ResourcesKey
|
||||
{
|
||||
private static Translations? _instance;
|
||||
|
||||
public static void Initialize(Translations translations)
|
||||
{
|
||||
_instance = translations;
|
||||
}
|
||||
|
||||
<#
|
||||
// Iterate through the dictionary and generate properties
|
||||
foreach (var translation in translations)
|
||||
{
|
||||
#>
|
||||
public static string <#= translation.Key #> => _instance?.<#= translation.Key #> ?? throw new InvalidOperationException("ResourcesKey.<#= translation.Key #> is not initialized.");
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
28
src/GameIdeas/GameIdeas.Resources/GameIdeas.Resources.csproj
Normal file
28
src/GameIdeas/GameIdeas.Resources/GameIdeas.Resources.csproj
Normal file
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="CreateStaticResourceKey.tt">
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
<LastGenOutput>CreateStaticResourceKey.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="CreateStaticResourceKey.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>CreateStaticResourceKey.tt</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
26
src/GameIdeas/GameIdeas.Resources/TranslationService.cs
Normal file
26
src/GameIdeas/GameIdeas.Resources/TranslationService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ArgosV2.Resources;
|
||||
|
||||
public class TranslationService
|
||||
{
|
||||
private readonly Dictionary<string, Dictionary<string, string>?> _translations = new();
|
||||
|
||||
public void Initialize(Dictionary<string, string> translations)
|
||||
{
|
||||
foreach (var translation in translations)
|
||||
{
|
||||
var json = JsonSerializer.Deserialize<Dictionary<string, string>>(translation.Value);
|
||||
_translations[translation.Key] = json;
|
||||
}
|
||||
}
|
||||
|
||||
public string Translate(string key, string? culture = "fr")
|
||||
{
|
||||
culture ??= CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
|
||||
if (_translations.TryGetValue(culture, out var value) && value?.TryGetValue(key, out var translate) == true)
|
||||
return translate;
|
||||
return key; // Fallback to key if translation is missing
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user